我是AndroidAsyncHttp的新手。
我创建了一个类 httptester :
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return relativeUrl;
}
在我的活动中做了以下工作:
RequestParams params = new RequestParams();
params.put("FTID", HTTPRequestUserAuthentication.AppID);
params.put("UUID", MainActivity.uuid);
params.put("TYPE", "11");
params.put("DateTimeStamp", DateTimeStamp);
params.put("SDFVersionNb", SDFVersionNb);
httptester.post(MainActivitySharedPref.GetValue(MyApplication.getContext(), "WebService_URL")+MyApplication.getContext().getResources().getString(R.string.url_get_user_data), params,new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
super.onSuccess(statusCode, headers, responseString);
Log.e(TAG, "sucess: " + responseString);
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Log.e(TAG, "failure: " + responseString);
Log.e(TAG, "failurecode: " + statusCode);
super.onFailure(statusCode, headers, responseString, throwable);
}
});
调用客户端后,将返回正确的响应,但它是在 OnFailure 中返回的,而不是在 OnSuccess 中返回的。 我还在 OnFailure 中打印了状态代码,它是 200,应该没问题。
任何帮助将不胜感激。
所以你调用你的请求
new JsonHttpResponseHandler()
但你需要
new AsyncHttpResponseHandler()