Android - loopJ AsyncHttpClient 返回响应 onFinish 或 onSuccess [英] Android - loopJ AsyncHttpClient return response onFinish or onSuccess
问题描述
我正在寻找一种方法来返回我在 loopJ AsyncHttpClient onFinish 或 onSuccess 或 onFailure 中得到的响应.截至目前,我有这段代码:
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;
}
}
当我调用代码时:
JSONParser jsonParser = new JSONParser();
jsonParser.getJSONFromUrl();
我在 onSuccess 或 onFailure 方法完成 http 帖子之前收到 JSONException.
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.
现在我正在寻找的是一种防止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 - loopJ AsyncHttpClient 返回响应 onFinish 或 onSuccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!