【问题标题】:Volley Request with Raw data带有原始数据的排球请求
【发布时间】:2016-02-23 08:21:52
【问题描述】:

在我的应用程序中,我必须使用 JSON req 参数发送 POST 请求,我尝试使用 Postman Rest Client 创建请求,它工作正常,但无法使用以下代码。

在 Postman req 参数中作为原始数据发送,但我不确定如何使用 Volley 请求发送。

public Request getHTTPPostReqResponse(String URL, Class mClass, final Map<String, String> params, final String contentType, final String body) {
    mResponseListener.requestStarted();

    Request mRequest = new GsonRequest(Request.Method.POST, URL, mClass, new Response.Listener<Object>() {

        @Override
        public void onResponse(Object response) {
            mResponseListener.requestCompleted(response);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            mResponseListener.requestEndedWithError(error);
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            return params;
        }

        @Override
        public byte[] getBody() throws AuthFailureError {
            if (TextUtils.isEmpty(body)){
                return super.getBody();
            }else {
                return  body.getBytes();
            }
        }

        @Override
        public String getBodyContentType() {
            return contentType;
        }

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

    mRequest.setRetryPolicy(new DefaultRetryPolicy(
            MY_SOCKET_TIMEOUT_MS,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    return mRequest;
}

请求参数:

{
    "nodeId": null,
    "userId": null,
    "mobileNumber": "993000008",
    "emailId": "sharma@gmail.com",
    "userProfile": null,
    "region": null,
    "countryCode": "91",
    "password": "pass@123",
    "places": [],
    "trustedNetwork": [],
    "profilePic": null,
    "fullName": null,
    "longitude": 0.0,
    "latitude": 0.0
}

【问题讨论】:

  • 你能更准确地描述一下这个问题吗?您可以发布您向 Postman 提出的请求吗?

标签: android android-volley androidhttpclient


【解决方案1】:

这是工作代码,你可以试试。我们可以将 JsonObjectRequest 用于原始数据。这里我只是展示了如何将 requestObject 添加到队列中

String url = MConstant.API_END+"/userLogin";
    Map<String, String> params = new HashMap<String, String>();
    params.put("email",mEmailView.getText().toString());
    params.put("password",mPasswordView.getText().toString());
    params.put("api",MConstant.API);
    JSONObject objRegData = new JSONObject(params);
    JsonObjectRequest jsObjRequest = new JsonObjectRequest
            (Request.Method.POST, url, objRegData, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {

                        // handle response data
                    } catch (JSONException e) {
                        e.printStackTrace();
                        //pDialog.show();
                    }

                }
            }, new Response.ErrorListener() {


                @Override
                public void onErrorResponse(VolleyError error) {
                    pDialog.hide();
                }
            })
    {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            return params;
        }
    };

在服务器端 PHP 我们可以使用

$json = file_get_contents('php://input');
$myPostData = json_decode($json, true);

【讨论】:

    【解决方案2】:

    希望这还不算太晚。

    您是否尝试过其他类型的请求,例如 String 或 JsonObject? 参数的语法是否不同?

    例如

         Map<String, Object> jsonParams = new ArrayMap<>();
        jsonParams.put("nodeId", null);
        jsonParams.put("userId", null);
        jsonParams.put("mobileNumber", "sharma@gmail.com");
        jsonParams.put("userProfile", null);
        jsonParams.put("region", null);
        jsonParams.put("countryCode", 91);
        jsonParams.put("password", pass@123);
        jsonParams.put("places", new ArrayList());
        jsonParams.put("trustedNetwork", new ArrayList());
        jsonParams.put("profilePic", null);
        jsonParams.put("fullName", null);
        jsonParams.put("longitude", 0.0);
        jsonParams.put("latitude", 0.0);
    
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(jsonParams),
                new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response)
                    {
                      mResponseListener.requestCompleted(response);
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        if (null != error.networkResponse)
                        {
                         mResponseListener.requestEndedWithError(error);
                        }
                    }
                });
    

    另外,看看this SO question。 希望这对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多