【发布时间】:2018-07-11 19:11:18
【问题描述】:
我的请求到达服务器,但我的 $_POST 在服务器端显示为空。我尝试了几种不同的方法,包括没有 Volley,但它仍然是空的。
这是我的 Android 代码:
public static void register(Context context){
String url = "https://cserra.com/gbc";
//Post parameters
final String action = "register";
final String name = text_name.getText().toString();
final String email = text_email.getText().toString();
final String facebook_id = "0";
final String auth_code = "";
final String os_id = "xxxx";
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG,response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG,error.getMessage());
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
HashMap<String, String> params2 = new HashMap<String, String>();
params2.put("action", action);
params2.put("name", name);
params2.put("email", email);
params2.put("facebook_id", facebook_id);
params2.put("auth_code", auth_code);
params2.put("os_id", os_id);
return new JSONObject(params2).toString().getBytes();
}
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
queue.add(sr);
}
这是我的 PHP 代码:
<?php
echo file_get_contents("php://input");
print_r($_POST);
这是我的回复:
D/MainActivity:数组 ( )
【问题讨论】:
-
这可能是服务器端,但是您是否尝试过使用 JsonObjectRequest 并将您的 JSONObject 有效负载作为参数传递给它,以查看该预构建类是否有效?它还可以防止您必须覆盖 StringRequest 的方法。
标签: php android post android-volley