簡體   English   中英

帶有 Volley 的 Android JSON 請求

[英]Android JSON request with Volley

我正在嘗試用 api 注冊一些數據,我檢查了我在應用程序中獲得的 json,結構和數據都很好,我把它放在 Postman 中,它工作正常,url 也很好,有人可以幫助我知道我做錯了什么?

這是我嘗試調用 api 的方法

private void request2(final String json){
    // Instantiate the RequestQueue.
    RequestQueue mRequestQueue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();

    // Request a string response from the provided URL.
    StringRequest stringRequestPOSTJSON = new StringRequest(Request.Method.POST, BASE_URL + "/api/Asociado/RegistrarAsociado", new Response.Listener() {
        @Override
        public void onResponse(Object response) {
            // response..
        }

    }, errorListener) {
        @Override
        public Priority getPriority() {
            return Priority.HIGH;
        }

        @Override
        public Map getHeaders() throws AuthFailureError {
            HashMap headers = new HashMap();
            headers.put("Authorization", "Bearer "+ bearertoken);
            return headers;
        }
        @Override
        public byte[] getBody() throws AuthFailureError {
            String your_string_json = json; // put your json
            return your_string_json.getBytes();
        }

    };
    // Add the request to the RequestQueue.
    mRequestQueue.add(stringRequestPOSTJSON);
}

這是我得到的錯誤: E/Volley: [4146] BasicNetwork.performRequest: Unexpected response code 400 for

試試這個代碼:

  stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                30000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM