首页 > retrofit2.0 post提交一直返回400,用httpurlconnection就可以

retrofit2.0 post提交一直返回400,用httpurlconnection就可以

1.用retrofit2.0 post提交 json
@POST("AppLoginIn")

Call<JsonObject> getLoginUser(@Body String parameterJson);

//待提交的json数据
private String toJsonUser() {

    JSONStringer jsonStringer = null;
    try {
        jsonStringer = new JSONStringer().object().key("LoginCode").value("SUPERMAN").key("Password")
                .value("111111").endObject();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.i("", "---json user:" + jsonStringer.toString());
    return jsonStringer.toString();
}
//retrofit 请求
 private void postYDL(String jsonString) {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    IService iService = retrofit.create(IService.class);
    Call<JsonObject> call = iService.getLoginUser(jsonString);
    call.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            String responseData = "---body:" + response.body()
    • "n---message:" + response.message()

    • "n---headers:" + response.headers()

    • "n---code:" + response.code()

    • "n---isSuccessful:" + response.isSuccessful();

                 Log.i("", "---base action responseData:" + responseData);
                 tv1.setText(responseData);
             }
      
             @Override
    1. void onFailure(Call<JsonObject> call, Throwable t) {

                 tv1.setText(t.getMessage());
             }
         });

      }

      换另一个请求地址结果:

    【热门文章】
    【热门文章】