【问题标题】:Android, HTTP POST request to Drupal back-endAndroid,对 Drupal 后端的 HTTP POST 请求
【发布时间】:2023-04-11 04:15:01
【问题描述】:

我在尝试通过 HTTP 请求实现通信时遇到问题。 更准确地说,我使用了 Volley 库,这是我的客户端请求:

StringRequest sr = new StringRequest(Request.Method.POST, "http://mysite/mydrupalservice",
                                        new Response.Listener<String>() {
                                            @Override
                                            public void onResponse(String response) {
                                                Log.w("CheckinFragment. POST call, RESPONSE:", String.valueOf(response));
                                            }
                                        },
                                        new Response.ErrorListener() {
                                            @Override
                                            public void onErrorResponse(VolleyError error) {
                                                VolleyLog.e("CheckinFragment. POST call, ERROR:", String.valueOf(error));
                                            }
                                        }){

                                    @Override
                                    protected Map<String,String> getParams()
                                    {
                                        Map<String,String> params = new HashMap<String, String>();
                                        params.put("action", "something");
                                        params.put("access_token", "something");
                                        params.put("activity", "1");
                                        return params;
                                    }

                                    @Override
                                    public Map<String, String> getHeaders() throws AuthFailureError
                                    {
                                        Map<String,String> headers = new HashMap<String, String>();
                                        headers.put("Accept", "application/json");
                                        headers.put("Content-Type","application/json; charset=utf-8");
                                        headers.put("Connection", "keep-alive");
                                        return headers;
                                    }
                                };

                                // add the request object to the queue to be executed
                                Controller.getInstance().addToRequestQueue(sr);`

这段代码在Android端正确执行,但收到​​的响应如下:

BasicNetwork.performRequest: Unexpected response code 404 for http://mysite/myservice
android.volley.AuthFailureError.

我尝试使用http://httpbin.org/ 来测试我的客户端代码,它可以正常工作。

所以,我认为问题可能出在服务器端。 如果我尝试使用 POST 参数执行 HTTP 请求,例如使用 Firefox 的扩展程序海报,我会收到来自服务器的正确响应。

查看 401 错误代码,我认为之前在 Drupal 上创建的服务需要用户进行身份验证,但该选项之前已禁用。

我不知道如何解决这个问题,有人知道吗? 感谢您的关注

【问题讨论】:

  • 您的 Drupal 后端有其他“简单”方法吗?可能先尝试调用 GET 方法,看看是否有错误发生。
  • 是的,我有几种方法。它们都用于 HTTP Get 请求。

标签: android drupal drupal-7 httprequest httpresponse


【解决方案1】:

也许尝试使用 url 编码?例如:

    @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"); // try this
        return params;
    }

【讨论】:

  • 我已经使用您的建议解决了我的问题,但还修改了在 drupal 上创建的服务中的“请求解析”选项,以便接受“application/x-www-form-urlencoded”输入类型。我不明白为什么drupal 不接受“application/json”输入类型
  • 很高兴能帮上忙。如果您使用 JsonObjectRequest 而不是 StringRequest,它可能会起作用。
猜你喜欢
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2015-03-08
  • 2023-03-28
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多