为什么JSON响应为200

时间:2019-08-23 12:29:23

标签: java android json android-volley

a)实际上,我有1个终结点,这些终结点将使用齐射发布到服务器上。另外我的api还具有自定义标头和自定义主体。问题是当我想获取JSON的响应时不会显示它。而是显示200。为此,我不使用表单,而只是从共享首选项中获取正文数据。

b)我还有其他端点使用自定义标头和正文,但使用的形式会将数据提交到服务器。 JSON响应正常,它将显示它。

a)JSON响应将显示200

//不同的代码是

jsonBody.put(“ tbid”,id);

id不是像b那样从edittext获取的,而是从共享首选项获取的,我确认id是合法的并获取数据。

b)将显示JSON响应

{ success:"1",message:"register" }

//这是b的代码

private void requestOTP(){
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);
    pDialog.setMessage("OTP OTW...");
    showDialog();

    try {
                    RequestQueue requestQueue = Volley.newRequestQueue(this);
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("tbid", str5);
        jsonBody.put("phone",phone.getText().toString());
        final String mRequestBody = jsonBody.toString();

        StringRequest strReq = new StringRequest(Request.Method.POST, URL_OTP_REQUEST, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e(TAG, "OTP Response: " + response);

                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    success = jObj.getInt(TAG_SUCCESS);

                    Log.e(Integer.toString(success),"SuccessRe");


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "OTP Error : " + error.getMessage());
                Toast.makeText(getApplicationContext()," OTP Error ",
                        Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }){

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

            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                    return null;
                }
            }



            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String responseString = "";
                if (response != null) {
                    responseString = String.valueOf(response.statusCode);
                }
                return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
            }


            /** Passing some request headers* */
            @Override
            public Map getHeaders() throws AuthFailureError {
                HashMap headers = new HashMap();
                headers.put("Auth-Key", "xxx");
                headers.put("Token", token);
                return headers;
            }


        };



        //Adding request to request queue

        requestQueue.add(strReq);

    }catch (JSONException e) {
        e.printStackTrace();
    }



}

c)我还使用端点创建了一个新项目,因为我认为问题可能是json响应是从项目中的其他端点获取的。但是没有任何改变。

d)已经使用邮递员,结果正常,它将显示b的json响应。

e)我的另一个选择是测试它是否正在使用异步任务或使用表单发布数据正文。

为什么我对a和b会得到不同的结果?这是服务器端还是客户端的问题?这是因为我没有使用表单发送数据主体吗?我知道200响应表示连接正常。但是我只想知道为什么我没有像a那样得到JSON响应。

齐射

1 个答案:

答案 0 :(得分:1)

这是因为方法parseNetworkResponse。请禁用此方法。

                @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String responseString = "";
                if (response != null) {
                    responseString = String.valueOf(response.statusCode);
                }
                return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
            }

肯定会在此之后得到json响应。