[英]Android Volley: BasicNetwork.performRequest: Unexpected response code 404
I want to get the user's information from database and display it, but I keep getting this error: 我想从数据库中获取用户的信息并显示它,但我不断收到此错误:
BasicNetwork.performRequest: Unexpected response code 404
BasicNetwork.performRequest:意外的响应代码404
Here's my javacode: 这是我的javacode:
loading method: 装载方式:
public void loadUserProfile() {
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
idTV.setText(jsonResponse.getString("_id"));
nameTV.setText(jsonResponse.getString("name"));
firstNameTV.setText(jsonResponse.getString("firstName"));
emailTV.setText(jsonResponse.getString("email"));
sexTV.setText(jsonResponse.getString("sex"));
yearTV.setText(jsonResponse.getString("year"));
cursusTV.setText(jsonResponse.getString("cursus"));
} catch (JSONException e) {
onLoadProfileFail();
}
}
};
ProfileRequest profileRequest = new ProfileRequest(user_id, responseListener);
RequestQueue queue = Volley.newRequestQueue(ProfileActivity.this);
queue.add(profileRequest);
}
Request class 请求类
class ProfileRequest extends StringRequest {
private static final String USER_PROFILE_REQUEST = "http://[ipaddress]:8080/users/:id";
private Map<String, String> params;
public ProfileRequest(String id, Response.Listener<String> listener) {
super(Method.POST, USER_PROFILE_REQUEST, listener, null);
params = new HashMap<>();
params.put("id", id);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
app.js app.js
router.get('/users/:id', function(req, res) {
User.find({ _id: "ie00847" }, function(err, user) {
if (err)
res.send(err);
res.json(user);
});
});
You got the 404 error since you're accessing a route with a method (POST) that is not available, because in the app.js
file you defined a function for a get request rather than a POST request so you need to update the Android code to a GET request. 自从您使用不可用的方法(POST)访问路由时出现404错误,因为在
app.js
文件中您为get请求而不是POST请求定义了一个函数,因此您需要更新Android代码到GET请求。 Something like the following, 像下面的东西,
class ProfileRequest extends StringRequest {
private static final String USER_PROFILE_REQUEST = "http://[ipaddress]:8080/users";
private Map<String, String> params;
public ProfileRequest(String id, Response.Listener<String> listener) {
super(Method.GET, USER_PROFILE_REQUEST + "/" + id, listener, null);
}
}
Also note that there is no need for overriding the getParams()
method as you're not sending a POST request anymore. 另请注意,不再需要覆盖
getParams()
方法,因为您不再发送POST请求。 In addition, you can't add parameters to a GET request except through hard-coding the values in the URL and not through a Map
object. 此外,除了通过对URL中的值进行硬编码而不是通过
Map
对象进行硬编码之外,您无法向GET请求添加参数。
Hope this helps :) 希望这可以帮助 :)
You can resolve it by using this code 您可以使用此代码解决它
public void onErrorResponse(VolleyError error) {
NetworkResponse response = error.networkResponse;
if (response != null && response.statusCode == 404) {
try {
String res = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Now you can use any deserializer to make sense of data
JSONObject obj = new JSONObject(res);
//use this json as you want
} catch (UnsupportedEncodingException e1) {
// Couldn't properly decode data to string
e1.printStackTrace();
} catch (JSONException e2) {
// returned data is not JSONObject?
e2.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.