java - 带有 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
解决方案
试试这个代码:
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
推荐阅读
- ckeditor - 将 Vue 组件插入 CKEditor 5 可编辑正文
- android - Unity Android 构建失败
- excel - 在没有循环的情况下结束 .FindNext 包装?
- java - 无法从码头发布请求中检索表单数据
- python - Django 查询过滤器语法问题
- database - 为什么vb停止执行一些代码并跳过它
- android - Android Kotlin coroutine crashing on strict mode
- bash - 嵌套 awk 从第二场比赛中抓取特定字符串并显示
- python - 在调用不带括号的类实例时返回一个对象
- ruby-on-rails - 如何将 Rails 模型文件拆分为多个文件(不是关注点,不是模块,只是单独的文件)?