gpt4 book ai didi

java - 尝试发布到自定义 API,收到 volley.ClientError

转载 作者:行者123 更新时间:2023-12-01 18:09:33 25 4
gpt4 key购买 nike

我正在尝试使用 volley 将一些数据发布到自定义 API。这是我也在尝试更新信息的 API 模型。可以在 http://ecoproduce.eu/swagger/index.html 上找到在以下 POST 链接下: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);

}

我尝试过寻找解决方案,但找不到任何有用的东西。是否会因为我尝试将 zipCode、state 和 account 作为字符串传递而发生错误,即使它们是模型中的整数?如果是这样,我如何将它们作为整数传递?目前我正在重写 getParams() 函数,它必须返回一个 Map 实例。如果这个问题有一个简单的解决方案,我想提前道歉,这是我第一次使用 RESTful API。

最佳答案

通过使用字符串请求解决。找到答案:How to send a POST request using volley with string body?

代码目前是这样的:

 private void addUser() {

JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();

try {
jsonObject.put("firstName", "Vasko");
jsonObject.put("lastName", "Vasilev");
jsonObject.put("email", "vaskovaskovasko@hotmail.com");
jsonObject.put("password", "18yoBonev");
jsonObject.put("zipCode", 0);
jsonObject.put("state", 1);
jsonObject.put("account", 2);

jsonArray.put(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}

RequestQueue requestQueue = Volley.newRequestQueue(this);
final String requestBody = jsonObject.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://ecoproduce.eu/api/User/register", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}

@Override
public byte[] getBody() {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};

requestQueue.add(stringRequest);
}

关于java - 尝试发布到自定义 API,收到 volley.ClientError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60492407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com