【发布时间】:2017-06-08 11:05:15
【问题描述】:
我目前正在尝试使用 CodeIgniter 访问我的数据库。但我无法从我的 StringRequest 方法传递参数。它说:
消息:未定义索引:电子邮件
这是我的 php 代码:
public function index() {
echo $_POST['email'];
}
这是我的字符串请求:
StringRequest request = new StringRequest(Request.Method.POST, SEND_REQUEST_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Volley Error", error.getMessage());
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("email", "test@test.com");
return params;
}
};
AppController.getInstance().addToRequestQueue(request);
我也试过补充:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
注意事项:
- 我知道使用 jsonrequest 传递参数时存在“错误”,因此我改用 stringrequest。
- 我已经重写了 getBodyContentType 方法。
- 不需要授权。
- 当我用 Postman 测试它时,它运行良好。
还有一点需要注意: 如果我将 url 打到一个普通的 php 文件以回显 POST 值,它就可以工作。但它就是无法进入我的 codeIgniter 中的 php 文件。
更新: 我发现虽然我在我的 android 应用程序中使用了 POST 方法,但不知何故,codeigniter 将它作为一种 GET 方法。我知道这一点使用
$_SERVER['REQUEST_METHOD']
【问题讨论】:
标签: android codeigniter android-volley