停止UI线程,直到http响应



我需要通过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

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
        }
    }

相关内容

  • 没有找到相关文章

最新更新