Android中使用Volley进行JSON请求

huangapple go评论77阅读模式
英文:

Android JSON request with Volley

问题

以下是翻译好的部分:

我正在尝试使用 API 注册一些数据,我已经检查了在我的应用程序中获取的 JSON,结构和数据都是正确的,我把它放在 Postman 中也可以正常工作,URL 也是正确的,有人可以帮助我了解我做错了什么吗?

这是我尝试调用 API 的方法:

private void request2(final String json){
    // 实例化请求队列。
    RequestQueue mRequestQueue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();

    // 从提供的 URL 请求字符串响应。
    StringRequest stringRequestPOSTJSON = new StringRequest(Request.Method.POST, BASE_URL + "/api/Asociado/RegistrarAsociado", new Response.Listener() {
        @Override
        public void onResponse(Object response) {
            // 响应处理...
        }

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

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + bearertoken);
            return headers;
        }
        
        @Override
        public byte[] getBody() throws AuthFailureError {
            String your_string_json = json; // 放入你的 JSON
            return your_string_json.getBytes();
        }
    };
    // 将请求添加到请求队列。
    mRequestQueue.add(stringRequestPOSTJSON);
}

这是我得到的错误:E/Volley: [4146] BasicNetwork.performRequest: 预期外的响应码 400

英文:

I am trying to register some data with an api and I have checked the json that I get in my application and the structure and data are fine, I put it in Postman and it works fine, the url is also fine, someone could help me to know what I am doing wrong ?

this is the method with which I try to make the call to the 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 + &quot;/api/Asociado/RegistrarAsociado&quot;, 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(&quot;Authorization&quot;, &quot;Bearer &quot;+ 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);
}

This is the error I get: E/Volley: [4146] BasicNetwork.performRequest: Unexpected response code 400 for

答案1

得分: 1

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

try this code:

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

huangapple
  • 本文由 发表于 2020年8月25日 08:43:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63570483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定