简体   繁体   English

获取 com.android.volley.ServerError

[英]Getting com.android.volley.ServerError

PUT request using volley使用 volley 的 PUT 请求

Log.i("JsonObject is",finaljsonData.toString());
Output:-

{"stats":[{"med_id":1,"med_name":"Evening","start_date":1476107306168,"end_date":1476193706168,"adherence":{"taken_on":[1476107704867],"missed_on":[]}}]}

Code:代码:

    JsonObjectHeader customRequest=new JsonObjectHeader(Request.Method.PUT,url, finaljsonData, listener, errorListener);
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    requestQueue.add(customRequest);

And the JsonObjectHeader class is:- JsonObjectHeader 类是:-

public class JsonObjectHeader extends JsonRequest<JSONObject> {

    public JsonObjectHeader(int method, String url, String requestBody, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, requestBody, listener, errorListener);
    }

    public JsonObjectHeader(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener,
                            Response.ErrorListener errorListener) {
        this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest,
                listener, errorListener);
    }

    public JsonObjectHeader(int method, String url, JSONObject jsonRequest,
                            Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                errorListener);
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {

        try {
            Log.i("Response parse","Yes");
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            Log.i("Json String",jsonString);
            Log.i("Response Complete",response.toString());
            Log.i("Response Data",response.data.toString());
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }

    }

    @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");
        headers.put("x-access-token", Constants.getTokenDB());
        return headers;

    }

}

LogCat OutPut: LogCat 输出:

10-10 19:45:15.186 22355-22426/user.com.test2 E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f5ba7cac7e0
10-10 19:45:15.797 22355-22650/user.com.test2 E/Volley: [198] BasicNetwork.performRequest: Unexpected response code 500 for url
10-10 19:45:15.798 22355-22355/user.com.test2 W/System.err: com.android.volley.ServerError
10-10 19:45:15.798 22355-22355/user.com.test2 W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
10-10 19:45:15.798 22355-22355/user.com.test2 W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

Server LogCat:-服务器 LogCat:-

{}
www-user-9 PUT /medstats 500 4.579 ms - 627

 TypeError: Cannot read property 'length' of undefined
www-user-9     at /abc.js:390:12
www-user-9     at Layer.handle [as handle_request] (/xyz.js.js:95:5)
www-user-9     at next (/abc.js:131:13)
www-user-9     at /abc.js:61:5
www-user-9     at //abc.js:27:18
www-user-9     at nextTickCallbackWith0Args (node.js:420:9)
www-user-9     at process._tickDomainCallback (node.js:390:13)

I have tried using PostMan.我尝试过使用 PostMan。 So it is working fine: so I think no server error所以它工作正常:所以我认为没有服务器错误

The json body you are sending as string, try to send it as a json object.您作为字符串发送的 json 正文,尝试将其作为 json 对象发送。 The server might be expecting a json object.服务器可能需要一个 json 对象。 Else there can be issue with the headers values.否则标题值可能会出现问题。 Check in postman whether the header that you are sending from there is same as what you are sending from the app.检查邮递员您从那里发送的标头是否与您从应用程序发送的标头相同。

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

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