java Android - loopJ AsyncHttpClient 返回响应 onFinish 或 onSuccess
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26938212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - loopJ AsyncHttpClient return response onFinish or onSuccess
提问by Ganesh Rathinavel
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:
我正在寻找一种方法来返回我在 loopJ AsyncHttpClient onFinish 或 onSuccess 或 onFailure 中得到的响应。截至目前,我有这段代码:
**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 JSONExceptionbefore 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()); is getting logged and then Log.d("onSuccess: ", jsonString); logs the value as they are in the sync state.
我注意到,在第一次调用时: Log.e("Exception", "JSONException" + e.toString()); 正在记录然后 Log.d("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 = new JSONObject(jsonString); 成功执行并且我获得了该方法所需的返回值,因为到那时 onSuccess 方法已经将值分配给变量 jsonString。
Now what I am exactly looking for is a way to prevent prematurereturn 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!
提前致谢!干杯!
回答by JstnPwll
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
}
});