Java 如何修复请求后改装时出现的错误400
无法发送Java 如何修复请求后改装时出现的错误400,java,android,retrofit,Java,Android,Retrofit,无法发送post请求,我使用postman尝试了生成的JSON,并在map中使用了适当的头。它工作得很好,但是我通过改装得到了错误400。这是API类 错误:E/onResponse:Response{protocol=http/1.1,code=400,message=Bad Request,url} 公共接口短信{ @邮政(“发送短信?国家=91”) 呼叫发送( @HeaderMap映射头, @体串obj ); } JSONObject object=new JSONObject();
post请求
,我使用postman
尝试了生成的JSON
,并在map
中使用了适当的头。它工作得很好,但是我通过改装得到了错误400
。这是API类
错误:E/onResponse:Response{protocol=http/1.1,code=400,message=Bad Request,url}
公共接口短信{
@邮政(“发送短信?国家=91”)
呼叫发送(
@HeaderMap映射头,
@体串obj
);
}
JSONObject object=new JSONObject();
JSONArray num=新的JSONArray();
JSONArray ar=新的JSONArray();
JSONObject sms=新的JSONObject();
试一试{
卖出数量(“9876543210”);
短信。放(“短信”,“你好吗?”);
sms.put(“to”,num);
ar.put(sms);
对象。put(“发送方”、“账单”);
反对。付诸表决(“路线”、“4”);
反对。付诸表决(“国家”、“91”);
object.put(“sms”,ar);
}捕获(JSONException e){
e、 printStackTrace();
}
改装改装=新改装.Builder()
.baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
SMS sms1=改装.create(SMS.class);
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json”);
headers.put(“authkey”、“29gsdg2ajsddfgdfgdfp25fdgb5”);
Call Call=sms1.send(headers,object.toString());
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.e(“onResponse”,即“+response.toString());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“onFailure”,即“+t.getMessage());
}
});
只需将您的国家/地区添加到@Query
参数中即可
像这样:
public interface SMS {
@POST("sendsms")
Call<ResponseBody> send (
@HeaderMap Map<String, String> headers,
@Body String obj,
@Query("country") int country
);
}
公共接口短信{
@邮政(“发送短信”)
呼叫发送(
@HeaderMap映射头,
@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(),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());
}
});
JSONObject object=new JSONObject();
JSONArray num=新的JSONArray();
JSONArray ar=新的JSONArray();
JSONObject sms=新的JSONObject();
试一试{
卖出数量(“9876543210”);
短信。放(“短信”,“你好吗?”);
sms.put(“to”,num);
ar.put(sms);
对象。put(“发送方”、“账单”);
反对。付诸表决(“路线”、“4”);
反对。付诸表决(“国家”、“91”);
object.put(“sms”,ar);
}捕获(JSONException e){
e、 printStackTrace();
}
改装改装=新改装.Builder()
.baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
SMS sms1=改装.create(SMS.class);
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json”);
headers.put(“authkey”、“29gsdg2ajsddfgdfgdfp25fdgb5”);
Call Call=sms1.send(headers,object.toString(),91);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.e(“onResponse”,即“+response.toString());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“onFailure”,即“+t.getMessage());
}
});
只需将您的国家/地区添加到@Query
参数中即可
像这样:
public interface SMS {
@POST("sendsms")
Call<ResponseBody> send (
@HeaderMap Map<String, String> headers,
@Body String obj,
@Query("country") int country
);
}
公共接口短信{
@邮政(“发送短信”)
呼叫发送(
@HeaderMap映射头,
@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(),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());
}
});
JSONObject object=new JSONObject();
JSONArray num=新的JSONArray();
JSONArray ar=新的JSONArray();
JSONObject sms=新的JSONObject();
试一试{
卖出数量(“9876543210”);
短信。放(“短信”,“你好吗?”);
sms.put(“to”,num);
ar.put(sms);
对象。put(“发送方”、“账单”);
反对。付诸表决(“路线”、“4”);
反对。付诸表决(“国家”、“91”);
object.put(“sms”,ar);
}捕获(JSONException e){
e、 printStackTrace();
}
改装改装=新改装.Builder()
.baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
SMS sms1=改装.create(SMS.class);
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json”);
headers.put(“authkey”、“29gsdg2ajsddfgdfgdfp25fdgb5”);
Call Call=sms1.send(headers,object.toString(),91);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.e(“onResponse”,即“+response.toString());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“onFailure”,即“+t.getMessage());
}
});
您可以尝试以下方法
public interface SMS {
@POST("sendsms?country=91")
Call<ResponseBody> send (
@HeaderMap Map<String, String> headers,
@Body JSONObject obj // replace string with JSONObject
);
}
....
// pass json object instead of string
Call<ResponseBody> call = sms1.send(headers, object);
如果您使用okhttpclient
传递头,那么您的API
端点应该如下所示
public interface SMS {
@POST("sendsms?country=91")
Call<ResponseBody> send (
@Body JSONObject obj // replace string with JSONObject
);
}
公共接口短信{
@邮政(“发送短信?国家=91”)
呼叫发送(
@Body JSONObject obj//用JSONObject替换字符串
);
}
希望对你有帮助快乐编码您可以尝试以下方法
public interface SMS {
@POST("sendsms?country=91")
Call<ResponseBody> send (
@HeaderMap Map<String, String> headers,
@Body JSONObject obj // replace string with JSONObject
);
}
....
// pass json object instead of string
Call<ResponseBody> call = sms1.send(headers, object);
如果您使用okhttpclient
传递头,那么您的API
端点应该如下所示
public interface SMS {
@POST("sendsms?country=91")
Call<ResponseBody> send (
@Body JSONObject obj // replace string with JSONObject
);
}
公共接口短信{
@邮政(“发送短信?国家=91”)
呼叫发送(
@Body JSONObject obj//用JSONObject替换字符串
);
}
希望对你有帮助快乐编码因此,您发送的数据似乎也有错误。签出你的API。我尝试记录数据并在POSTMAN中使用它。成功了。你是如何在POSTMAN中将数据添加到你的请求中的?结帐,是你吗