简体   繁体   English

Android-LoopJ AsyncHttpClient返回响应onFinish或onSuccess

[英]Android - loopJ AsyncHttpClient return response onFinish or onSuccess

I am looking for a way to return the response I get in loopJ AsyncHttpClient onFinish or onSuccess or onFailure. 我正在寻找一种方法来返回我在loopJ AsyncHttpClient onFinish或onSuccess或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;
        }
}

When I invoke the code: 当我调用代码时:

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

I get the JSONException before onSuccess or onFailure method finishes the http post. 在onSuccess或onFailure方法完成http发布之前,我得到了JSONException

I have noticed that, on the first invoke: Log.e("Exception", "JSONException " + e.toString()); 我注意到,在第一次调用时:Log.e(“ Exception”,“ JSONException” + e.toString()); is getting logged and then Log.d("onSuccess: ", jsonString); 正在获取日志,然后Log.d(“ onSuccess:”,jsonString); logs the value as they are in the sync state. 记录值,因为它们处于同步状态。

On the second invoke: jObj = new JSONObject(jsonString); 在第二次调用时: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. 成功执行并获得所需的方法返回值,因为到那时,onSuccess方法已经将值分配给了jsonString变量。

Now what I am exactly looking for is a way to prevent premature return of jObj from the method. 现在,我正在寻找的是一种防止jObj从该方法过早返回的方法。

Is there anyway to make the method, getJSONObj, wait for the completion of AsyncHttpClient task, assign the variable into jsonString, create the JSONObject and return it? 无论如何,有没有方法getJSONObj,等待AsyncHttpClient任务完成,将变量分配给jsonString,创建JSONObject并返回它?

Thanks in advance! 提前致谢! Cheers! 干杯!

Use an interface. 使用界面。 This way you can create your own callback whose methods can be called from onSuccess or onFailure. 这样,您可以创建自己的回调,其回调方法可以从onSuccess或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());
       }
   }
}

And to call it: 并称之为:

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

Use retrofit . 使用改造 It manages the http request very good and converts the json objects automatically to your Java objects. 它可以很好地管理http请求,并将json对象自动转换为Java对象。 There is also a failure and success Callback. 还有一个失败和成功的回调。 You can also decide whether the request is async or sync. 您还可以决定请求是异步还是同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM