[英]AsyncHttpClient post and get JSON response
I have to post a JSON to server and the server retrieves me another. 我必须将JSON发布到服务器,并且服务器会检索另一个。 I tried this but it doesn't work. 我试过了,但是没有用。 When I debug, I never see the logs in on Success or onFailure: 调试时,我从不会看到登录成功或失败的日志:
String url = "https://*******/sessions";
JSONObject requestObject = new JSONObject();
try {
requestObject.put("company", "TEST");
requestObject.put("user", "*****");
requestObject.put("secure_password", "8ce241e1ed84937ee48322b170b9b18c2");
requestObject.put("secure_device_id", "C4CA4238A0B923820DCC509A6F75849B");
} catch (JSONException e) {
e.printStackTrace();
}
// JSONObject jsonParams = new JSONObject();
// jsonParams.put("notes", "Test api support");
StringEntity entity = null;
try {
entity = new StringEntity(requestObject.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
client.post(this, url, entity, "application/json",
new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String res = responseBody.toString();
Log.e("response " , res);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
How I can get the response body? 我如何获得响应机构?
I don't understand clearly what you are asking, but you will never see the response because you can't convert the reponseBody
to String like String res = responseBody.toString();
我不清楚您在问什么,但您永远不会看到响应,因为您无法像String res = responseBody.toString();
那样将 reponseBody
转换为String String res = responseBody.toString();
. 。
Do String res = new String(responseBody);
做 String res = new String(responseBody);
instead. 代替。
If that's not the problem update your question and I'll update my answer. 如果不是那样,请更新您的问题,然后我将更新我的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.