凌空请与原始数据 [英] Volley Request with Raw data

查看:142
本文介绍了凌空请与原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序必须发送POST请求,JSON REQ参数,我试着用邮差REST客户端创建请求,并将其工作正常,但工作不低于code。

在发送的原始数据邮差REQ参数,但我不知道如何将它与凌空请求发送。

 公开征求getHTTPPostReqResponse(字符串URL,类mClass,最终地图<字符串,字符串>参数,可以最终字符串的contentType,最后弦乐体){
    mResponseListener.requestStarted();

    请求mRequest =新GsonRequest(Request.Method.POST,网址,mClass,新Response.Listener<对象>(){

        @覆盖
        公共无效onResponse(对象响应){
            mResponseListener.requestCompleted(响应);
        }
    },新Response.ErrorListener(){

        @覆盖
        公共无效onErrorResponse(VolleyError错误){
            mResponseListener.requestEndedWithError(错误);
        }
    }){
        @覆盖
        保护地图<字符串,字符串> getParams()方法{
            返回PARAMS;
        }

        @覆盖
        公共字节[] getBody()抛出AuthFailureError {
            如果(TextUtils.isEmpty(体)){
                返回super.getBody();
            }其他 {
                返回body.getBytes();
            }
        }

        @覆盖
        公共字符串getBodyContentType(){
            返回的contentType;
        }

        @覆盖
        公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
            地图<字符串,字符串> PARAMS =新的HashMap<>();
            params.put(内容类型,则contentType);
            返回PARAMS;
        }
    };

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

    返回mRequest;
}
 

请求参数:

  {
    节点ID:空,
    用户id:空,
    移动电话号码:993000008,
    EMAILID:sharma@gmail.com,
    用户配置:空,
    区域:空,
    国code:91,
    密码:通过@ 123,
    地方:[],
    trustedNetwork:[],
    profilePic:空,
    全名:空,
    东经:0.0,
    纬度:0.0
}
 

解决方案

希望这还不算晚。

您是否尝试过不同类型的要求,如字符串或JSONObject的? 和不同的语法PARAMS?

例如。

 地图<字符串,对象> jsonParams =新ArrayMap<>();
    jsonParams.put(节点ID,NULL);
    jsonParams.put(用户id,NULL);
    jsonParams.put(移动电话号码,sharma@gmail.com);
    jsonParams.put(用户配置,NULL);
    jsonParams.put(区域,NULL);
    jsonParams.put(国code,91);
    jsonParams.put(密码,通过@ 123);
    jsonParams.put(地方,新的ArrayList());
    jsonParams.put(trustedNetwork,新的ArrayList());
    jsonParams.put(profilePic,NULL);
    jsonParams.put(全名,NULL);
    jsonParams.put(经度,0.0);
    jsonParams.put(纬度,0.0);

    JsonObjectRequest请求=新JsonObjectRequest(Request.Method.POST,网址,新的JSONObject(jsonParams)
            新Response.Listener<的JSONObject>()
            {
                @覆盖
                公共无效onResponse(JSONObject的响应)
                {
                  mResponseListener.requestCompleted(响应);
                }
            },
            新Response.ErrorListener()
            {
                @覆盖
                公共无效onErrorResponse(VolleyError错误)
                {
                    如果(NULL!= error.networkResponse)
                    {
                     mResponseListener.requestEndedWithError(错误);
                    }
                }
            });
 

另外,看看<一href="http://stackoverflow.com/questions/28172496/android-volley-how-to-isolate-requests-in-another-class/30604191#30604191">this SO质疑。 希望任何这会有所帮助。

In my application I have to send POST request with JSON req param, I tried to create request with Postman Rest Client and it is working fine but not working with below code.

In Postman req parameter sent as raw data, But I am not sure how to send it with Volley request.

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;
}

Request Parameter:

{
    "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
}

解决方案

hope this isn't too late.

Have you tried a different type of request, like String or JsonObject? And a different syntax for the params?

e.g.

     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);
                    }
                }
            });

Also, have a look at this SO question. Hope any of this helps.

这篇关于凌空请与原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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