我正在制作一个android应用程序,需要发送Post请求到我的服务器,可能真的需要一些帮助!我一直在尝试使用android Volley发出这些请求。我在这里读了很多关于android Volley的相关问题和各种文档来源,但当我设法提出我的请求时,我不可能在服务器端以正确的方式接收发布的数据,我也不知道我做错了什么……我尝试过使用stringRequest覆盖getParams()和自定义JsonObjectRequest,并将我的数据作为参数。我在服务器端得到了两个请求的不同结果,但我仍然不能以正确的方式捕获它。
在我的服务器端,我的调试PHP文件是这样的:
$content = file_get_contents("php://input");
$string = 'fileGetContents: ';
$json = json_decode($content, TRUE);
$string = 'fileGetContents: ';
$string .= $content;
$string .= ' -- json encoded: ';
$string .= json_encode($json);
$string .= ' -- Request Method: ';
$string .= $_SERVER['REQUEST_METHOD'];
$string .= ' -- $_REQUEST: ';
$string .= json_encode($_REQUEST);
$string .= ' -- $_POST: ';
$string .= json_encode($_POST);
$string .= ' -- user: ';
$string .= json_encode($_POST["user"]);
$string .= ' -- $_GET: ';
$string .= json_encode($_GET);
$string .= ' -- $_FORM: ';
$string .= json_encode($_FORM);
$array["ans"] = $string;
echo json_encode($array);
由于我得到的响应总是空的,所以我也尝试将我的请求发送到,它会给我提供有趣的信息。
因此,对于我创建的第一个stringRequest:
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, "myServer.com/postRequest.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
displayTheResponse(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RegisterActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user", "test");
params.put("pass", "passtest");
return params;
}
};
requestQueue.add(stringRequest);
我从我的服务器上得到了这个答案:
{"ans":"fileGetContents: -- json encoded: null -- Request Method: GET -- $_REQUEST: [] -- $_POST: [] -- user: null -- $_GET: []"}
从httpbin.org我得到的是:
{ "args": {},
"data": "",
"files": {},
"form": { "pass": "passtest", "user": "test" },
"headers": { "Accept-Encoding": "gzip", "Content-Length": "24", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Host": "httpbin.org", "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 7.1; Android SDK built for x86 Build/NPF26K)" },
"json": null,
"origin": "104.221.22.144",
"url": "http://httpbin.org/post"
}
我可以看到信息实际上是发送的,但不是作为POST数据接收的,而是作为表单数据发送的,我真的不知道它是什么,也不知道如何从PHP接收它……
然后在我的第二个JsonObjectRequest中是这样的:
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("user", "test");
JsonObjectRequest myRequest = new JsonObjectRequest(
Request.Method.POST,
"myServer.com/postTest.php",
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
displayResponse(response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LogInActivity.this, "Error "+error.getMessage(), Toast.LENGTH_LONG).show();
fm.popBackStack();
}
});
requestQueue.add(myRequest);
从httpbin.org我得到:
{ "args": {},
"data": "{\"user\":\"test\"}",
"files": {},
"form": {},
"headers": { "Accept-Encoding": "gzip", "Content-Length": "15", "Content-Type": "application\/json; charset=utf-8", "Host": "httpbin.org", "User-Agent": "Dalvik\/2.1.0 (Linux; U; Android 7.1; Android SDK built for x86 Build\/NPF26K)" },
"json": { "user": "test" },
"origin": "104.221.22.144",
"url": "http:\/\/httpbin.org\/post"
}
在这里,我们看到数据显示在" data“和"json”中,而不是"form“中,但是我仍然从我的服务器得到相同的空响应,所以我猜我的问题来自于我正在使用的服务器端PHP代码。我还注意到“$_SERVER‘’REQUEST_METHOD‘”返回"GET“。
我已经尝试了很多不同的方法来发出截击请求,但我不知道还能做什么。我尝试过创建一个帮助器类,自定义JsonObjectRequest,覆盖getHeaders(),甚至用AppController将请求添加到队列中……我想我对帖子请求的工作原理没有足够的理解,我对android开发还很陌生。
如果您能帮助我理解如何在服务器端使用Volley POST方法接收我的数据,我将非常感激。非常感谢您的宝贵时间!
发布于 2018-06-23 04:12:04
下面的代码可以用来在android中使用Volley库发出POST请求。假设您在应用程序build.gradle文件中以同样的方式导入库:
dependencies{
....
implementation 'com.android.volley:volley:1.1.0'
.....
}
下面的代码可以作为执行POST请求的例程任务放入一个方法中
String requestUrl = "http://myapi.com/api/postresource.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, requestUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Volley Result", ""+response); //the response contains the result from the server, a json string or any other object returned by your server
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace(); //log the error resulting from the request for diagnosis/debugging
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> postMap = new HashMap<>();
postMap.put("param1", "value1");
postMap.put("param2", value2);
//..... Add as many key value pairs in the map as necessary for your request
return postMap;
}
};
//make the request to your server as indicated in your request url
Volley.newRequestQueue(getContext()).add(stringRequest);
https://stackoverflow.com/questions/41517397
复制相似问题