繁体   English   中英

把不工作在 android Volley org.json.JSONException: End of input at character 0 of

[英]put not working on android Volley org.json.JSONException: End of input at character 0 of

我向邮递员发送了它的工作请求,但截击不起作用。 我总是出错! 当响应为空时,我搜索了 stackoverflow volley 返回错误,但我添加了CustomJsonObjectRequest问题仍然存在。

错误信息

Volley org.json.JSONException: End of input at character 0 of

自定义对象请求

public class CustomJsonObjectRequest extends JsonObjectRequest {

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

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            if (response.data.length == 0) {
                byte[] responseData = "{}".getBytes("UTF8");
                response = new NetworkResponse(response.statusCode, responseData, response.headers, response.notModified);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return super.parseNetworkResponse(response);
    }
}

凌空请求

EcoElement ecoElement = ecoElementArrayList.get(position);

                Map<String, String> params = new HashMap<String, String>();

                params.put("Id", ecoElement.getId().toString());
                params.put("Checked", ecoElement.getChecked().toString());

                JSONObject ObjParams = new JSONObject(params);



                try {
                    CustomJsonObjectRequest getRequest = new CustomJsonObjectRequest(Request.Method.PUT, "https://ourwebsite.sslbeta.de/api/gardenapi/updateecoelements", ObjParams,
                            response -> {
                                Toast.makeText(getContext(), ""+response, Toast.LENGTH_SHORT).show();
                                progressBar.setVisibility(View.GONE);
                            },
                            error -> {
                                Toast.makeText(getContext(), ""+error.toString(), Toast.LENGTH_SHORT).show();
                                progressBar.setVisibility(View.GONE);
                            }
                    );
                    RequestQueueSingleton.getInstance(getContext()).addToRequestQueue(getRequest);
                } catch (Exception e) {
                    Toast.makeText(getContext(), e.toString(), Toast.LENGTH_LONG).show();
                }

在此处输入图片说明

这是解决方案,只需创建此方法并传递您的值即可。

private void CustomJsonObjectRequest() {

    String tag_string_req = "req__details";

    StringRequest strReq = new StringRequest(Request.Method.POST, <API URL>, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            getPerspective().showErrorLogs(TAG, "Response Invest : " + response);

            try {
                
                // Parse your response here
                
                
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Id", <ID VALUE HERE>);
            params.put("Checked", <TRUE or FALSE>);
            return params;
        }
    };

    strReq.setRetryPolicy(new RetryPolicy() {

        @Override
        public void retry(VolleyError arg0) throws VolleyError {
        }

        @Override
        public int getCurrentTimeout() {
            return 0;
        }

        @Override
        public int getCurrentRetryCount() {
            return 0;
        }
    });
    strReq.setShouldCache(false);
    addToRequestQueue(strReq, tag_string_req);
}

ID 和 Checked 都必须具有 String 类型。并在 MainActivity 中创建以下方法:

 private RequestQueue mRequestQueue;

 public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

经过数小时的搜索,我终于能够解决问题。 这有两个原因,首先是 api 返回空/空,因此CustomJsonObjectRequest修复了该问题,然后另一个问题是我忘记添加身份验证标头。 我知道这是一个愚蠢的错误!

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json");
                headers.put("Authorization", "Bearer "+access_token);
                return headers;
            }
        };

暂无
暂无

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

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