【问题标题】:How to post json with volley to php web service如何将 json 与 volley 发布到 php web 服务
【发布时间】:2014-12-14 23:28:07
【问题描述】:

我正在尝试使用 volley 将 Json 发布到我的宁静 API,但它不起作用。到目前为止,我已经通过 chrome 上的 Advance rest 客户端应用程序发送一个 json 有效负载来测试我的 Web 服务,它返回一个 json 响应....但是当我用 Volley 尝试它时,它返回 onErrorResponse。谁能告诉我如何解决这个问题,在此先感谢。

Json 负载:

   {"country":"isreal","mobile":"009988778"}

代码

 private void processLogin() {
    showProgressDialog();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Const.LOGIN_URL, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.i("JSON response", "JSON Posting" + response.toString());
            hideProgessDialog();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            VolleyLog.d(ERROR_TAG, "Error: " + volleyError.getMessage());
            hideProgessDialog();
        }
    }){

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

    @Override
    protected Map<String, String> getParams(){
        String country_value = country.getSelectedItem().toString();
        String mobile_value = mobile.getText().toString();
        Map<String, String> params = new HashMap<>();
        params.put("country",country_value);
        params.put("mobile", mobile_value);
        return params;
    }
    };

    AppController.getInstance().addToRequestQueue(jsonObjReq, login_tag);

}

【问题讨论】:

  • 编辑您的问题以包含常见的 JSON 有效负载,该负载可通过 Chrome 中的高级 REST 客户端工作,但不能通过 Volley 工作。

标签: java php android json android-volley


【解决方案1】:

您正在做的是,您尝试将 JSON 作为标头的一部分发送,但这是行不通的。

这就是你需要的-

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Const.LOGIN_URL, **->YOUJSONOBJECTHERE<-**, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.i("JSON response", "JSON Posting" + response.toString());
            hideProgessDialog();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            VolleyLog.d(ERROR_TAG, "Error: " + volleyError.getMessage());
            hideProgessDialog();
        }
    });

您需要将JSONObject 作为请求的一部分而不是作为请求标头的一部分发送。试试看,如果它解决了问题,我们会解决它。

由于您已经在使用JSONObjectRequest,您既不需要设置内容类型也不需要覆盖getParams(),除非您需要在标头中发送一些额外信息,例如 AgentType、令牌等。

【讨论】:

  • 我很高兴,它奏效了。请考虑接受答案,以便对其他人有所帮助。
【解决方案2】:
  1. 将对象转换为 json 字符串。
  2. 覆盖 Volley 请求的 getBody 对象
  3. 覆盖 getBodyContentType 以设置为 json

      Gson gson = new Gson();
      final String json = gson.toJson(loginDTO);
    
    
      GsonRequest<EmailLoginRestResponse> jsObjRequest = new GsonRequest<EmailLoginRestResponse>(
                Request.Method.POST, url,
                EmailLoginRestResponse.class, null,
                this.createLoginRequestSuccessListener(),
                this.createLoginErrorListener()){
    
            @Override
            public byte[] getBody() throws AuthFailureError {
                return json.getBytes();
            }
    
            @Override
            public String getBodyContentType() {
                return "application/json; charset=" + this.getParamsEncoding();
            }
        };
    
        jsObjRequest.setShouldCache(false);
        this.mRequestQueue.add(jsObjRequest);
    

GsonRequest.java

https://github.com/ogrebgr/android_volley_examples/blob/master/src/com/github/volley_examples/toolbox/GsonRequest.java

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    相关资源
    最近更新 更多