我正在制作一个 Android 应用程序,需要将 Post 请求发送到我的服务器,并且确实需要一些帮助!
我一直在尝试使用 android Volley 提出这些请求。我在这里阅读了很多相关问题以及有关 android Volley 的各种文档来源,但是当我设法提出请求时,我不可能在服务器端以正确的方式接收发布的数据,而且我不知道我做错了什么...
我尝试过字符串请求压倒性的获取参数()和定制Json对象请求我的数据作为范围。我在服务器端收到两个请求的不同结果,但我仍然无法以正确的方式捕获它。
在我的服务器端我的调试PHP file是这样的:
$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);
由于我得到的回复总是空的,我也尝试将我的请求发送到http://httpbin.org/post这给了我有趣的信息。
所以与第一个字符串请求我这样做:
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 接收它......
然后在我的第二个Json对象请求事情是这样的:
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”。
我尝试了很多不同的方法来提出 Volley 请求,但我不知道还能做什么。我尝试过创建一个辅助类、自定义 JsonObjectRequest、重写 getHeaders(),甚至使用 AppController 将请求添加到队列中...我想我对 Post 请求的工作方式没有足够的了解,我很新随着android的开发。
我真的非常感谢任何帮助我了解我应该如何使用 Volley POST 方法在服务器端接收数据的帮助。非常感谢您的宝贵时间!