我需要通过php脚本从服务器获取数据 我正在使用loopj库中的AsyncHttpClient和AsyncHttpResponseHandler。
public void buttonListener (View view) {
if (view.getId() == R.id.button) {
//start loading...
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://host.com/data.php", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//loading succeeded
//now I can parse the byte[] responseBody to a JSONObject...
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//loading failed
}
});
//I want my program to stop at this point until onSuccess() or onFailure() is called
}
}
在程序中描述的那一点,我想等到服务器做出响应。我找到了一些使用Threads
和方法wait()
和notifyAll()
的示例,但我不知道如何在我的情况下使用它们。
有人能帮助我吗?
THX
答案 0 :(得分:0)
public void buttonListener (View view) {
if (view.getId() == R.id.button) {
//start loading...
AsyncHttpClient client = new AsyncHttpClient();
Dialog dialog = new Dialog(MainActivity.this);
dialog.setTitle("");
// show a dialog that can't be close by user.
final ProgressDialog progressDialog = new ProgressDialog(view.getContext());
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
client.get("http://host.com/data.php", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//loading succeeded
//close the dialog
//now I can parse the byte[] responseBody to a JSONObject...
progressDialog.dismiss();
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//loading failed
//close the dialog
progressDialog.dismiss();
}
});
//I want my program to stop at this point until onSuccess() or onFailure() is called
}
}