如何在POST请求上修复错误400

时间:2019-09-12 11:46:33

标签: java android retrofit

无法发送Enter a number<input id="number" type="text" onkeyup="decimalCheck()" style="direction: rtl;"></input>,我用#!/usr/bin/env bash mapfile -d '' java_sources < <(find . -type f -iname '*.java' -print0) javac -g "${java_sources[@]}" -classpath "${JAVA_CLASSPATH}" mapfile -d '' java_classes < <(find . -type f -iname '*.class' -print0) jar cf "${JAVA_DIST}/${JAR_FILE}" "${java_classes[@]}" rm -- "${java_classes[@]}" 中适当的标题尝试了post request生成的JSON。它工作正常,但我将postmanmap一起使用。这是error 400

  

错误:E / onResponse:响应{协议= http / 1.1,代码= 400,消息=错误的请求,网址}

retrofit

API class

2 个答案:

答案 0 :(得分:0)

只需将您的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());
        }
    });

答案 1 :(得分:0)

您可以尝试以下操作。

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传递标题,如下所示

    private static Retrofit getClient() {
        if (retrofit == null) {
            String baseUrl = "your_base_url";
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .client(okHttpClient) // add okhttpclient here
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

    // create OkHttpClient for adding headers.
    private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(@NonNull Chain chain) throws IOException {
                    Request request = chain.request().newBuilder().addHeader("Content-Type", "application/json").addHeader("authkey", "29gsdg2AJsddfgddfP25fdgb5").build();
                    return chain.proceed(request);
                }
            })
            .build();

如果您使用okhttpclient传递标头,则您的API端点应类似于

public interface SMS {
@POST("sendsms?country=91")
Call<ResponseBody> send (
    @Body JSONObject obj // replace string with JSONObject 
    );
}

希望它对您有帮助。 快乐编码