1

我必须发送

{ "200": {"username": "ppoo" ,
                     "password": "ppoo" ,
                     "emails": ["ppoo"] },
            "400": [] } 

在我的请求的原始正文中,它会在成功响应时给出状态代码 200。

如果我使用 JSONobjectrequest,那么它将显示 VolleyParseError。我不知道使用哪个请求。

4

1 回答 1

1

完整的工作代码在这里..

private void LoginUser() {
           try {jsonObject.put("password",hashpassword );jsonObject.put("username",username);} catch (JSONException e){e.printStackTrace();}
    jsonStr =jsonObject.toString(); 

//json 字符串变量 Hold the this is your raw data that you want to send data in string looks ={"username":"your user username","password":"yourpassword"}//

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest    = new StringRequest(Request.Method.POST, SignInUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(getBaseContext(),response.toString(), Toast.LENGTH_LONG).show();
                    hideProgressDialog();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("VOLLEY", error.toString());
                    Toast.makeText(getBaseContext(),error.toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }
        @Override
        public byte[] getBody() {
            try {
                return jsonStr == null ? null : jsonStr.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                        jsonStr, "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));
        }
        @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;
        }};
    requestQueue.add(stringRequest);

}

于 2016-05-21T07:22:04.837 回答