【问题标题】:Android volley StringRequest POST method failed to pass paramsAndroid volley StringRequest POST 方法无法传递参数
【发布时间】: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;
}

注意事项:

  1. 我知道使用 jsonrequest 传递参数时存在“错误”,因此我改用 stringrequest。
  2. 我已经重写了 getBodyContentType 方法。
  3. 不需要授权。
  4. 当我用 Postman 测试它时,它运行良好。

还有一点需要注意: 如果我将 url 打到一个普通的 php 文件以回显 POST 值,它就可以工作。但它就是无法进入我的 codeIgniter 中的 php 文件。

更新: 我发现虽然我在我的 android 应用程序中使用了 POST 方法,但不知何故,codeigniter 将它作为一种 GET 方法。我知道这一点使用

$_SERVER['REQUEST_METHOD']

【问题讨论】:

    标签: android codeigniter android-volley


    【解决方案1】:
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    
            StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
    
                    System.out.println(response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
    
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email", "test@test.com");
                    return params;
                }
            };
    
            requestQueue.add(request);
    

    希望这篇文章对你有帮助

    【讨论】:

    • 感谢您的帮助,但我当前的代码实际上已经使用了这种模式,因为 AppController 是我创建的一个管理 volley requestqueue 的类
    猜你喜欢
    • 2020-03-18
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2017-07-19
    相关资源
    最近更新 更多