gpt4 book ai didi

java - okHttpClient 响应给出垃圾值

转载 作者:行者123 更新时间:2023-12-03 00:56:37 25 4
gpt4 key购买 nike

我正在尝试使用 okHttpClient 4.2.1 库注册用户,作为响应,我得到了一些垃圾值

Respone is  �������������V*.MNN-.V�JK�)N�Q�r�S���rR�SSR��K2��t�RK���ck�NY�<������

我使用了 postman ,其响应正是我所期望的。我不明白为什么它在 Android Studio 中不起作用。

{"result": {"OTP": 1113401845,"id": 143},"success": true,"message": "User Registered In Successfully"}

这是我使用的代码

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json; charset=utf-8");

JSONObject postdata = new JSONObject();
try {
postdata.put("action", "registrationFirst");
postdata.put("User_name", "abcd");
postdata.put("password", "12345");
postdata.put("email", "aaaa3@a.com");
postdata.put("street", "qa");
postdata.put("Gender", "1");
postdata.put("Religion", "3");
postdata.put("caste", "Other");
postdata.put("Country", "UAE");
postdata.put("mobile", "0123456");
postdata.put("Locaion", "SHJ");
} catch(JSONException e){
// TODO Auto-generated catch block
e.printStackTrace();
}

RequestBody body = RequestBody.create(mediaType,postdata.toString());

Request request = new Request.Builder()
.url(myUrl)
.post(body)
.addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic aW5mb0Bhbm9vbnouY29tOjEyMTIxMg==")
.addHeader("User-Agent", "PostmanRuntime/7.20.1")
.addHeader("Accept", "application/json")
.addHeader("Cache-Control", "no-cache")
.addHeader("Postman-Token", "cf631a82-2570-4c20-9c39-be0563240c67,f9cecf3d-2340-4fce-bb6f-c060c9f8894f")
.addHeader("Accept-Encoding", "gzip, deflate")
.addHeader("Content-Length", "1278")
.addHeader("Connection", "keep-alive")
.addHeader("cache-control", "no-cache")
.build();

Response response = null;

try {
response = client.newCall(request).execute();

if (response.isSuccessful()) {

String result = response.body().string();
Log.d("Hola","Response is "+result);

}

} catch (Exception e) {
e.printStackTrace();
Log.d("Hola","Failed exception "+e);

}

最佳答案

使用 Request.Builder 构建请求时,您应该删除以下行:

addHeader("Accept-Encoding", "gzip, deflate")

当您指定自己的 Accept-Encoding 值时,您暗示您希望自己进行解压缩,但您的情况并非如此。

关于java - okHttpClient 响应给出垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59347697/

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