使用Volley的发布方法不起作用 [英] Post Method using Volley not working

查看:72
本文介绍了使用Volley的发布方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Volley作为我的登录页面.我需要像这样传递数据

Hi i am using Volley for my login page. I need to pass data like this manner

{
userID : '-988682425884628921',
email :'aditya@vyas.com',
passwd : '123ss'
}

我正在使用POST方法发送数据,我已经在DHC中签入,Api在DHC中工作正常,并且我正在 JSON响应,但是当我尝试使用Volley时,我无法获得响应.甚至在我的logcat中也没有任何错误.

I am using POST Method to send data,I already check in DHC, The Api is working fine in DHC and I am getting JSON Response, But when i try with Volley i am not able to get response. and not even any error in my logcat.

JAVA代码

 RequestQueue mVolleyQueue = Volley.newRequestQueue(this);

    CustomRequest req = new CustomRequest(Request.Method.POST,url,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.v("tag","login response " + response);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.v("tag","login error response " + error.getMessage());
        }
    }){
        @Override
        public Map<String, String> getParams() throws AuthFailureError {
            Map<String, String>  params = new HashMap<String, String>();

            params.put("userID", "-988682425884628921");
            params.put("email", "aditya@vyas.com");
            params.put("passwd", "123ss");
            return params;
        }


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

错误

05-28 09:20:14.696 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError is ! null
05-28 09:20:14.697 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError status code : 400
05-28 09:20:14.697 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError message : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Header</h2>
<hr><p>HTTP Error 400. The request has an invalid header name.</p>
</BODY></HTML>


    }

推荐答案

解决了您的问题.刚使用过JsonArrayRequest并以JsonObject形式传递参数:

Solved your problem. Just used JsonArrayRequest and passed parameters in JsonObject form:

    Map<String, String> params = new HashMap<String, String>();
    params.put("userID", "userid");
    params.put("email","email");
    params.put("passwd", "password");
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, "url", new JSONObject(params),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    System.out.println("response -->> " + response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    System.out.println("change Pass response -->> " + error.toString());
                }
            });

    request.setRetryPolicy(new

            DefaultRetryPolicy(60000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


    Volley.newRequestQueue(activity).add(request);

无需覆盖getParams()getHeaders().

No need of overriding getParams() or getHeaders().

问题:1 您得到响应码500 ,因为服务器接受params作为JsonObject,并且我们正在尝试提供String.

Problem : 1 You were getting response code 500 because the server was accepting the params as JsonObject and we are trying to feed String.

问题:2 您正在使用JsonObjectRequet,但是服务器的响应位于JsonArray中,因此您需要使用JsonArrayRequest接受JsonArray

Problem : 2 You were using JsonObjectRequet but the response from the server was in JsonArray so you need to use JsonArrayRequest to accept the response in JsonArray

尝试让我知道这对您有帮助或不帮助:)

Try and let me know this helps or not :)

这篇关于使用Volley的发布方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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