簡體   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