本文介绍了Android的 - 循环J AsyncHttpClient返回响应onFinish或的onSuccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一种方式来回报我得到了循环J AsyncHttpClient onFinish或的onSuccess或onFailure响应。截至目前我有这块code:

I am looking for a way to return the response I get in loopJ AsyncHttpClient onFinish or onSuccess or onFailure. As of now I have this piece of code:

**jsonParse.java file**
public class jsonParse {
    static JSONObject jObj = null;
    static String jsonString = "";
    AsyncHttpClient client;

      public JSONObject getJSONObj() {
            RequestParams params;
            params = new RequestParams();
            params.add("username", "user");
            params.add("password", "password");
            client = new AsyncHttpClient();
            client.post("http://example.com", params, new TextHttpResponseHandler() {

                @Override
                public void onSuccess(int i, Header[] headers, String response) {
                    jsonString = response;
                    Log.d("onSuccess: ", jsonString);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String response, Throwable e) {
                    if (statusCode == 401) {
                        jsonString = response;
                        Log.d("onFailure: ", jsonString);
                    }
                }

            });
            try {
                jObj = new JSONObject(jsonString);
            } catch (JSONException e) {
                Log.e("Exception", "JSONException " + e.toString());
            }
            return jObj;
        }
}

当我调用code:

JSONParser jsonParser = new JSONParser();
jsonParser.getJSONFromUrl(); 

我得到的 JSONException 之前的onSuccess或onFailure方法完成的HTTP POST。

I get the JSONException before onSuccess or onFailure method finishes the http post.

我已经注意到,在第一次调用:Log.e(异常,JSONException+ e.toString());是越来越记录,然后Log.d(的onSuccess:,jsonString);日志的值,因为它们是在同步状态

I have noticed that, on the first invoke: Log.e("Exception", "JSONException " + e.toString()); is getting logged and then Log.d("onSuccess: ", jsonString); logs the value as they are in the sync state.

在第二个调用: jObj =新的JSONObject(jsonString);得到成功执行,我得到期望的回报值的方法,因为这时的onSuccess方法将已分配的值赋予变量jsonString。

On the second invoke: jObj = new JSONObject(jsonString); gets executed successfully and I get desired return value for the method, because by that time onSuccess method would have already assigned the value to the variable jsonString.

现在什么,我究竟在寻找一种方法来prevent premature 从方法返回jObj的。

Now what I am exactly looking for is a way to prevent premature return of jObj from the method.

反正是有使该方法,getJSONObj,等待完成AsyncHttpClient任务,分配变量到jsonString,创建的JSONObject并返回它?

在此先感谢!干杯!

推荐答案

使用的接口。这样,您就可以创建自己的回调,其方法可以从的onSuccess或onFailure被调用。

Use an interface. This way you can create your own callback whose methods can be called from onSuccess or onFailure.

public interface OnJSONResponseCallback {
    public void onJSONResponse(boolean success, JSONObject response);
}

public JSONObject getJSONObj(OnJSONResponseCallback callback) {
    ...
   @Override
   public void onSuccess(int i, Header[] headers, String response) {
       try {
           jObj = new JSONObject(response);
           callback.onJSONResponse(true, jObj);
       } catch (JSONException e) {
           Log.e("Exception", "JSONException " + e.toString());
       }
   }

   @Override
   public void onFailure(int statusCode, Header[] headers, String response, Throwable e) {
       try {
           jObj = new JSONObject(response);
           callback.onJSONResponse(false, jObj);
       } catch (JSONException e) {
           Log.e("Exception", "JSONException " + e.toString());
       }
   }
}

和调用它:

jsonParse.getJSONObj(new OnJSONResponseCallback(){
    @Override
    public void onJSONResponse(boolean success, JSONObject response){
       //do something with the JSON
    }
});

这篇关于Android的 - 循环J AsyncHttpClient返回响应onFinish或的onSuccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:55