在Volley中实现POST请求时出现错误代码400,但在使用OkHttp实现时则不行

时间:2017-10-14 15:23:56

标签: android android-volley okhttp3

下面给出的是我在使用OkHttp时用来发送POST请求的类:

public class PostExample {
private static final MediaType JSON
        = MediaType.parse("application/json;");

private OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    try (Response response = client.newCall(request).execute()) {
        return response.body().string();
    }
}

String bowlingJson(String user, String password) {
    String str =  "{" +
            "\"username\": \"" + user + "\", " +
            "\"password\": \"" + password + "\"" +
            "}";
    System.out.println(str);
    return str;
}

public static void main(String[] args) throws IOException {
    PostExample example = new PostExample();
    String json = example.bowlingJson(args[0], args[1]);
    String response = example.post("http://192.168.43.123:8000/api/jwt-auth/", json);
    System.out.println(args[0] + " " + args[1]);
    System.out.println(response);
}
}

这很好用,它给了我一个JSON对象作为响应中的字符串。 下面给出的是与Volley一起使用的几乎相同的东西,只是它给我一个400错误的差异

公共类PostAssistant {

    private Context context;
    private RequestQueue queue;

    public PostAssistant(Context context) {
        this.context = context;
        queue = Volley.newRequestQueue(context);
    }

    private void post(String url, final String user , final String password) throws IOException {
        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response) {
                        // response
                        Log.d("Response", response);
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error
                        Log.d("Error.Response", "");
                    }
                }
        )
        {
            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String> params = new HashMap<>();
                params.put("username", user);
                params.put("password", password);

                return params;
            }

            @Override
            public String getBodyContentType()
            {
                return "application/json";
            }
        };
        queue.add(postRequest);
    }

    private void connect(String user, String password) throws IOException {
        post(Constants.loginUrl , user , password);
    }

    public void connect(String[] args) throws IOException {
        connect(args[0] , args[1]);
    }
}

这是logcat中显示的错误:

E/Volley: [5322] BasicNetwork.performRequest: Unexpected response code 400 for http://192.168.43.123:8000/api/jwt-auth/

1 个答案:

答案 0 :(得分:0)

我通过将getBodyContentType()更改为getHeaders()

来修复此问题
        @Override
        public Map<String , String> getHeaders()
        {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            return params;
        }