英文:
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 + "/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);
}
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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论