[英]Volley JSONObjectRequest Request.Method.POST but received GET
I made a Custom JSONObjectRequest as below code. 我做了一个自定义JSONObjectRequest如下代码。
Currently i made the Request.Method.POST
and i had confirmed it while passing to the super class here super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
目前,我发出了
Request.Method.POST
并在传递给此处的超类时进行了确认super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
. 。 It was received as
int 1
(POST), but when received on my server (PHP), the log was receiving as "REQUEST_METHOD":"GET"
它以
int 1
(POST)的形式接收,但是在我的服务器(PHP)上接收时,日志的接收形式为"REQUEST_METHOD":"GET"
Have anyone met this kind of problem or can help to point out what i had missed in below code. 有没有人遇到过这种问题,或者可以帮助指出以下代码中我错过的内容。 As you can see below, i had make sure below things:
正如您在下面看到的,我已经确定以下几点:
params
not empty params
不为空 getParams()
there and the params
are not empty getParams()
并且params
不为空 getBody()
and make sure it not returning null getBody()
并确保它不返回null Refer below code for what i had tried to do 请参阅以下代码以了解我尝试做的事情
try {
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
if (jsonObject != null && jsonObject.length() > 0) {
try {
if (jsonObject.getInt("status") == ResponseCode.LOGIN_FAILED) {
SharedManager sharedManager = new SharedManager();
sharedManager.logoutUser(context, true);
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
callback.onSuccessResponse(jsonObject);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callback.onErrorResponse(error);
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
try {
return JSONHelperConverter.jsonToMapString(params);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8;";
}
@Override
public byte[] getBody() {
try {
String postBody = null;
try {
postBody = createPostBody(JSONHelperConverter.toMapString(params));
postBody = params.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return postBody == null ? null : postBody.getBytes("utf-8");
} catch (NegativeArraySizeException n) {
n.printStackTrace();
return null;
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
return null;
}
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
String responseString;
JSONObject jsonObject = null;
if (response != null) {
try {
responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
jsonObject = new JSONObject(responseString);
} catch (Exception e) {
e.printStackTrace();
}
}
return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
}
};
NetworkSingleton.getInstance(context).addToRequestQueue(stringRequest);
} catch (NegativeArraySizeException n) {
n.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
I solved my own problem Sent POST request but server says GET request in Android volley, what I am doing wrong here? 我解决了自己的问题发送了POST请求,但是服务器在Android Volley中说GET请求,我在这里做错了什么?
Solution: Due to i'm requesting to PHP server, i need to point out the filename. 解决方案:由于我正在请求PHP服务器,因此我需要指出文件名。
Currently i point the request to a URL without Filename extension, and i just add index.php
to my end of URL and it worked. 目前,我将请求指向不带文件扩展名的URL,我只是将
index.php
添加到URL的末尾,它可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.