在Android中使用POST处理原始json数据时传递的空参数

时间:2019-04-30 04:41:14

标签: android

我正在制作一个新的android应用程序,我是一个初学者。从我的登录活动开始,我正在尝试使用POST发送参数。但是当我调试时,我发现没有参数被传递。在堆栈溢出中尝试了几乎所有答案,没有一个解决了我的问题。谁能帮忙

           private void jsonRequestLogin() {
    try {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String URL = Constants.LOGIN_URL;
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("Username", uName.getText().toString().trim());
        jsonBody.put("Password", paswd.getText().toString().trim());
        final String requestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("VOLLEY", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

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

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String responseString = "";
                if (response != null) {
                    responseString = String.valueOf(response.statusCode);
                    // can get more details such as response.headers
                }
                return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
            }
        };

        requestQueue.add(stringRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

3 个答案:

答案 0 :(得分:0)

使用getParams()方法来请求参数

private void check() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //Log.d(TAG, "onResponse:" + response);

                        try {
                            JSONArray responseArray = new JSONArray(response);
                            JSONObject jsonObject = responseArray.getJSONObject(0);
                            //Log.d(TAG, "onResponse: jsonArray " + responseArray);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Log.d(TAG, "onErrorResponse: " + error);
                Toast.makeText(SplashScreenActivity.this, "Some error occurred try after sometimes", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("param1",param1);
                params.put("param2", param2;
                //Log.d(TAG, "getParams: " + params);
                return params;
            }
        };
        addToRequestQueue(stringRequest, "");

    }

答案 1 :(得分:0)

尝试覆盖headers方法,并将内容类型设置为init。

@Override
   public Map<String, String> getHeaders() throws AuthFailureError {

     HashMap<String, String> headers = new HashMap<String, String>();
     headers.put("Content-Type", "application/json; charset=utf-8");
     return headers;
  }

答案 2 :(得分:0)

我认为您应该使用翻新

Retrofit

Retrofit 2