gpt4 book ai didi

java - AsyncHttpClient 发布并获取 JSON 响应

转载 作者:行者123 更新时间:2023-12-02 13:13:08 25 4
gpt4 key购买 nike

我必须将 JSON 发布到服务器,然后服务器会检索另一个 JSON。我尝试过这个,但它不起作用。当我调试时,我从未在成功或失败时看到日志:

       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) {

}
});

如何获取响应正文?

最佳答案

我不太明白你在问什么,但你永远不会看到响应,因为你无法将 reponseBody 转换为像 String 这样的字符串res = responseBody.toString(); .

执行 String res = new String(responseBody); 改为。

如果这不是问题,请更新您的问题,我会更新我的答案。

关于java - AsyncHttpClient 发布并获取 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43868771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com