首页 > 解决方案 > 带有 Volley 的 Android JSON 请求

问题描述

我正在尝试使用 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

标签: javaandroidjsonapiandroid-volley

解决方案


试试这个代码:

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

推荐阅读