如何通过volley jsonObjectRequest post mehod传递一个字符串?

时间:2017-07-21 10:56:03

标签: android post android-volley

这给了我错误,因为它只期望JSON对象作为参数。有没有办法在POST请求中传递字符串?我需要传递一个加密的字符串。

 public JsonObjectRequest addContact(String url, final String contactString, final AddContactCallback addContactCallback) {

     final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, contactString, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            if (response.length() > 0 && response.getString("status").equalsIgnoreCase("1")) {
                                if (response.getString("message").equalsIgnoreCase("success")) {// registration

                                    addContactCallback.onAddContactRequestSuccess(....);

                                } 
                            }else {
                                addContactCallback.onAddContactRequestError(new VolleyError());
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            addContactCallback.onAddContactRequestError(new VolleyError());
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (error instanceof NetworkError || error instanceof NoConnectionError) {
                            addContactCallback.onNetworkError();
                        } else {
                            addContactCallback.onAddContactRequestError(error);
                        }
                    }
                });
                jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                        10000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                return jsonObjectRequest;
            }

3 个答案:

答案 0 :(得分:1)

我假设你想要在帖子请求的主体中传递一个参数,这在排球中有这个方法

  @Override
        protected Map<String,String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("stringKey", YOUR_ACTUAL_STRING);

            return params;
        }

适合您的情况

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, contactString, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        if (response.length() > 0 && response.getString("status").equalsIgnoreCase("1")) {
                            if (response.getString("message").equalsIgnoreCase("success")) {// registration

                                addContactCallback.onAddContactRequestSuccess(....);

                            } 
                        }else {
                            addContactCallback.onAddContactRequestError(new VolleyError());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        addContactCallback.onAddContactRequestError(new VolleyError());
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (error instanceof NetworkError || error instanceof NoConnectionError) {
                        addContactCallback.onNetworkError();
                    } else {
                        addContactCallback.onAddContactRequestError(error);
                    }
                }
            }){
              @Override
              protected Map<String,String> getParams(){
              Map<String, String> params = new HashMap<String, String>();
              params.put("stringKey", YOUR_ACTUAL_STRING);
               return params;
    };
            jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                    10000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            return jsonObjectRequest;
        }

答案 1 :(得分:0)

如果你想通过jordObjectRequest post methord将字符串发送到服务器,那么你需要让密钥和值对通过服务器发送

喜欢:

JSonObject jsonobjest=new JSonObject();
jsonObject.put(Key_name,String_value);

并使用与此处相同的jsonObjectRequest在服务器上发送此jsonobject。 它的工作我,你可以使用它。

答案 2 :(得分:0)

我得到了solution.override getBody方法并将字符串作为byteArray传递给JsonObjectRequest构造函数传递null代替jsonobject

{
            @Override
            public byte[] getBody() {

                byte[] body = new byte[0];
                try {
                    body = contactString.getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {

                }
                return body;
            }
        };