gpt4 book ai didi

java - 如何修复 POST 请求 Retrofit 上的错误 400

转载 作者:行者123 更新时间:2023-11-30 05:02:56 27 4
gpt4 key购买 nike

无法发送post request ,我试了一下生成的JSONpostman map 中带有适当的头.它工作正常,但我得到 error 400retrofit .这里是 API class

Error : E/onResponse: Response{protocol=http/1.1, code=400, message=Bad Request, url}


public interface SMS {
@POST("sendsms?country=91")
Call<ResponseBody> send (
@HeaderMap Map<String, String> headers,
@Body String obj
);
}
   JSONObject object = new JSONObject();
JSONArray num = new JSONArray();
JSONArray ar = new JSONArray();
JSONObject sms = new JSONObject();
try {
num.put("9876543210");

sms.put("message", "Hi How are you?");
sms.put("to", num);

ar.put(sms);

object.put("sender", "BILLWT");
object.put("route", "4");
object.put("country", "91");
object.put("sms", ar);

} catch (JSONException e) {
e.printStackTrace();
}

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

SMS sms1 = retrofit.create(SMS.class);

HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("authkey", "29gsdg2AJsddfgddfP25fdgb5");

Call<ResponseBody> call = sms1.send(headers, object.toString());

call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.e("onResponse ", " " + response.toString());
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("onFailure ", " " + t.getMessage());
}
});

最佳答案

只需添加您的 country@Query范围。

像这样:

public interface SMS {
@POST("sendsms")
Call<ResponseBody> send (
@HeaderMap Map<String, String> headers,
@Body String obj,
@Query("country") int country
);
}

它将根据您的需要自动添加。

您的代码更改如下:
JSONObject object = new JSONObject();
JSONArray num = new JSONArray();
JSONArray ar = new JSONArray();
JSONObject sms = new JSONObject();
try {
num.put("9876543210");

sms.put("message", "Hi How are you?");
sms.put("to", num);

ar.put(sms);

object.put("sender", "BILLWT");
object.put("route", "4");
object.put("country", "91");
object.put("sms", ar);

} catch (JSONException e) {
e.printStackTrace();
}

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

SMS sms1 = retrofit.create(SMS.class);

HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("authkey", "29gsdg2AJsddfgddfP25fdgb5");

Call<ResponseBody> call = sms1.send(headers, object.toString(),91);

call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.e("onResponse ", " " + response.toString());
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("onFailure ", " " + t.getMessage());
}
});

关于java - 如何修复 POST 请求 Retrofit 上的错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57906043/

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