使用截击发送原始发布请求

时间:2015-05-05 15:13:25

标签: android android-volley

我想用原始字符串发送post请求,而不是使用volley设置params。

我试图在StringRequest中覆盖getBody方法,如下所示:

@Override
public byte[] getBody() throws AuthFailureError {
    return rawString.getBytes();
}

它甚至不会发送请求并给出错误:com.android.volley.TimeoutError

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:14)

我得到了......

   RequestQueue queue = Volley.newRequestQueue(this);

    queue.add(myReq);

    ...

        StringRequest myReq = new StringRequest(Request.Method.POST,
                        server+"Login",
                        createMyReqSuccessListener(),
                        createMyReqErrorListener()) {

                    @Override
                    public byte[] getBody() throws com.android.volley.AuthFailureError {
                        String str = "{\"login\":\""+login+"\",\"password\":\""+pass+"\"}";
                        return str.getBytes();
                    };

                    public String getBodyContentType()
                    {
                        return "application/json; charset=utf-8";
                    }

                };

        ...

            private Response.Listener<String> createMyReqSuccessListener() {
                return new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i(TAG,"Ski data from server - "+response);
                    }
                };
            }


            private Response.ErrorListener createMyReqErrorListener() {
                return new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i(TAG,"Ski error connect - "+error);
                    }
                };
            }