Android Volley:意外的响应代码405 [英] Android Volley: Unexpected response code 405
问题描述
我的Android Volley JsonObjectRequest遇到onErrorResponse问题:
My Android Volley JsonObjectRequest runs into onErrorResponse with the issue:
BasicNetwork.performRequest: Unexpected response code 405 for MY_URL
我的URL有效.我已经用浏览器检查过了 我到了预期的JSON对象. 因此,问题必须在客户端.
My URL is valid. I have checked that with a browser and I get there the expected JSON Object. So the issue has to be on the client side.
代码405的意思是:
不允许的方法不允许在请求行中指定方法 允许使用Request-URI标识的资源.响应 必须包含一个Allow标头,其中包含用于以下目的的有效方法的列表 请求的资源.
Method Not Allowed The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.
我的JsonObjectRequest代码:
my code for JsonObjectRequest:
JsonObjectRequest jsonReq;
jsonReq = new JsonObjectRequest(URL_FEED, new JSONObject(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.v("ERROR:%n %s", error.getMessage());
}
});
// Adding request to volley request queue
NetworkController.getInstance().addToRequestQueue(jsonReq);
我必须在标题中添加一些信息吗?如果要提供信息呢?
Do I have to add some information to the header? And if what for information?
推荐答案
问题是默认情况下该请求设置为POST
.
适用于我的解决方案:
The issue was that the request was set to POST
by default.
The solution that worked for me:
JsonObjectRequest jsonReq = new JsonObjectRequest
(Request.Method.GET, URL_FEED, null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
Log.d("Server", "Läuft");
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.d("Server","onErrorResponse");
}
});
NetworkController.getInstance().addToRequestQueue(jsonReq);
这篇关于Android Volley:意外的响应代码405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!