java - 尝试发布到自定义 API,收到 volley.ClientError
问题描述
我正在尝试使用 volley 将一些数据发布为自定义 API。这也是我正在尝试更新信息的 API 模型。它可以在以下 POST 链接下的http://ecoproduce.eu/swagger/index.html上找到:http: //ecoproduce.eu/api/User/register/
{
"firstName": "string",
"lastName": "string",
"email": "string",
"password": "string",
"zipCode": 0,
"state": 0,
"account": 0
}
当我尝试向 API 添加新的测试用户时,我收到以下错误:
E/Volley: [9583] BasicNetwork.performRequest: Unexpected response code 400 for http://ecoproduce.eu/api/User/register/
E/Error response: com.android.volley.ClientError
这是我用来发布用户的功能。
private void addUser() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest arrayRequest = new JsonArrayRequest(
Request.Method.POST,
"http://ecoproduce.eu/api/User/register/",
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error response", error.toString());
}
})
{
@Override
protected Map<String,String> getParams() {
Map<String,String> params = new HashMap<String,String>();
params.put("firstName", "Vasko");
params.put("lastName", "Vasilev");
params.put("email", "vasko@hotmail.com");
params.put("password", "18yoBonev");
params.put("zipCode", "0");
params.put("state", "1");
params.put("account", "2");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
requestQueue.add(arrayRequest);
}
我已经尝试寻找解决方案,但我找不到任何有用的东西。是否会发生错误,因为我试图将邮政编码、状态和帐户作为字符串传递,即使它们是模型中的整数?如果是这样,我怎样才能将它们作为整数传递?目前我正在覆盖必须返回 Map 实例的 getParams() 函数。如果这个问题有一个简单的解决方案,我想提前道歉,这是我第一次使用 RESTful API。
解决方案
Volley 不适用于 https 请求,也许这个链接可以帮助你:Volley with Https
或者android:usesCleartextTraffic="true"
,如果您想授权 Http 纯文本,请将此行添加到您的清单中。
推荐阅读
- regex - sed to edit a line on a file
- django - Django 查询集对象查询花费太多时间?
- python - 在 for 循环中向前 x 个项目添加不同的特定项目,而不使用 enumerate
- regex - 需要一个不接受第二个点的十进制数的正则表达式
- azure - 不支持关键字:@microsoft.keyvault
- javascript - 在 Node.js 模块中使用函数
- java - 如何修复 Talend 中的“日期格式”问题
- c# - 如何在 UWP(通用 Windows 编程)应用程序中进行简单的用户登录
- curl - 无法从托管它的服务器内部 curl 到站点,它返回 403 Forbidden,但是从外部到站点使用 curl 很好
- c++ - 如何将参数 -fvisibility=default 添加到 CMAKE?