Android Volley发送json数据获取错误

时间:2016-11-01 10:21:03

标签: android android-volley

我尝试使用volley notwork库登录 'com.android.volley:volley:1.0.0'

如果我正在使用硬编码json对象参数,它的工作正常,但万一从EditText获取值得到服务器错误

错误跟踪

11-01 17:07:10.937 8456-8750/com.furdo.android E/Volley: [277] BasicNetwork.performRequest: Unexpected response code 400 for https://url/
11-01 17:07:10.938 8456-8456/com.furdo.android D/Volley: [1] override.onErrorResponse: Furdo:Proceed
11-01 17:07:10.938 8456-8456/com.furdo.android W/System.err: com.android.volley.ServerError
11-01 17:07:10.938 8456-8456/com.furdo.android W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
11-01 17:07:10.938 8456-8456/com.furdo.android W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
11-01 17:07:11.549 8456-8536/com.furdo.android E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab6e9480

jsonObject.put(“mobile_no”,str_email_or_phone); //从editText获取值不起作用 jsonObject.put(“mobile_no”,“9999999999”); //硬编码工作

Even String var =“value”具有相同的错误

String str_email_or_phone = et_enter_no_or_email.getText().toString().trim();
JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("mobile_no", str_email_or_phone);// getting value from editText not working
        jsonObject.put("mobile_no", "9999999999");// Hard code working

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    if (jsonObject.length() >0) {
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, Api.BASE_URL + Api.CHECK_USER, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
                    }
                }, new Response.ErrorListener() {


            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                // hide the progress dialog
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Accept", "application/www.inception.v1");
                params.put("Content-Type", "application/json");
                return params;
            }
        };
        // Adding request to request queue
        MyApplication.getInstance().addToRequestQueue(jsonObjReq, "tag");
    }

1 个答案:

答案 0 :(得分:1)

从堆栈跟踪中可以清楚地看到,您从服务器获得400响应。你的客户端(安卓代码)没问题。

检查您的服务器,了解为什么它对虚拟请求的响应正常,但对于有效请求则为400。