使用JSON身体StringRequest [英] StringRequest with JSON body

查看:182
本文介绍了使用JSON身体StringRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送使用凌空请求,但我想不出如何使它工作。

I'm trying to send a request using Volley but I can't figure how to make it work.

我需要使用JSON发送POST请求连接codeD数据为体,但尝试不同的事物小时后我仍然不能使它发挥作用。

I need to send a POST request with JSON encoded data as the body, but after hours of trying different things I still can't make it work.

这是我目前的code的请求:

This is my current code for the request:

User user = User.getUser(context);
String account = user.getUserAccount();
String degreeCode = user.getDegreeCode();

final JSONObject body = new JSONObject();
try {
    body.put(NEWS_KEY, 0);
    body.put(NEWS_DEGREE, degreeCode);
    body.put(NEWS_COORDINATION, 0);
    body.put(NEWS_DIVISION, 0);
    body.put(NEWS_ACCOUNT, account);
} catch (JSONException e) {
    e.printStackTrace();
}

StringRequest request = new StringRequest(Request.Method.POST, GET_NEWS, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(String response) {
        Log.i(TAG, response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "Error: " + getMessage(error, context));
        Toast.makeText(context, getMessage(error, context), Toast.LENGTH_SHORT).show();
    }
}) {
    @Override
    public byte[] getBody() throws AuthFailureError {
        return body.toString().getBytes();
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");
        return headers;
    }
};
queue.add(request);

但是,这code总是返回错误的请求错误

But this code always returns "Bad request error"

有些事情,我已经试过:

Some things I've tried:


  • 覆盖 getParams()方法方法,而不是 getBody()。 (没有工作)

  • 发送 JSONObjectRequest 上的构造体。这其中的工作,但因为我的Web服务返回字符串价值,我总是得到一个 ParseError 。这就是为什么我使用 StringRequest

  • Override getParams() method instead of getBody(). (Didn't work)
  • Send a JSONObjectRequest with the body on the constructor. This one worked, but because my web service returns a String value I always get a ParseError. That's why I'm using StringRequest.

任何帮助非常多AP preciated。

Any help is very much appreciated.

推荐答案

href=\"http://stackoverflow.com/questions/27451094/stringrequest-with-json-body#comment43340939_27451094\">njzk2's评论,最简单的方法是重写 getBodyContentType()来代替。覆盖 getHeaders()或许可以工作太多,但你需要把所有必需的头,不仅内容类型,因为你基本上覆盖原来的方法设置的头。

As already mentioned on njzk2's comment, the easiest way is to override getBodyContentType() instead. Overriding getHeaders() could probably work too, but you need to put all necessary headers, not only Content-Type, since you basically override the headers that the original method set.

您code应该是这样的:

Your code should look like this:

StringRequest request = new StringRequest(...) {
    @Override
    public byte[] getBody() throws AuthFailureError {
        return body.toString().getBytes();
    }

    @Override
    public String getBodyContentType() {
        return "application/json";
    }
};

这篇关于使用JSON身体StringRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆