繁体   English   中英

Android Volley请求返回状态码200以响应成功,并要求以Json原始格式发送数据(正文)

[英]Android Volley Request return status code 200 in response of success and require to send data in Json as raw (body)

我必须发送

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

在我的请求的原始内容中,成功响应时将给出状态代码200。

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

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

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

// json字符串变量保留这是您要以字符串形式发送数据的原始数据= {“ 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);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM