Android Volley Request using parameters to Wordpress using JSON API

Android Volley Request using parameters to Wordpress using JSON API

我目前在我的 android 应用程序中使用 HttpClient/HttpResponse 调用来传输和接收数据到我的 wordpress 网站,它工作得很好。我在网站上使用 https://wordpress.org/plugins/json-api/ 插件来处理请求。 Google 正在尝试逐步淘汰那些 http 协议,并建议改用 volley 进行联网。所以我正在尝试进行转换,但是对服务器的查询 post 参数不知何故被拒绝了。

使用此处的代码作为起点: http://code.tutsplus.com/tutorials/creating-a-weather-application-for-mars-using-volley--cms-23812

private String WORDPRESS_API_ENDPOINT = "http://example.com/api/mobileapp/get_posts?id=123&title=test";   // example url with query parameters
    CustomJsonRequest request = new CustomJsonRequest
            (Request.Method.POST, WORDPRESS_API_ENDPOINT, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {

                        // Do stuff with the returned data

                    } catch (Exception e) {
                        txtError(e);
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    txtError(error);
                }
            });

    request.setPriority(Request.Priority.HIGH);
    helper.add(request);
}

在我的服务器 php 代码上,我正在打印 $_SERVER($_POST 和 $_GET 为空)全局变量以查看传入请求并显示以下内容(仅显示数组中的相关条目):

array (
            'REQUEST_URI' => '/api/mobileapp/get_posts/',
            'REQUEST_SCHEME' => 'http',
            'CONTEXT_PREFIX' => '',
            'REMOTE_PORT' => '13326',
            'REDIRECT_URL' => '/api/mobileapp/get_posts/',
            'REQUEST_METHOD' => 'GET',
            'QUERY_STRING' => ''
    )

请注意,Request_uri 已从查询参数中删除,我无法获得所需的正确 post。我已经尝试调试 volley 代码并通读了很多 SO posts 以正确的方式通过 volley 传递正确的 json 数据,但是 none 的方法有效. 它似乎出现在服务器端某处的错误,因为如果我使用不同的 api url(例如 flickr),我就能成功调用。 与 httpclient 调用相比,我需要对我的服务器进行哪些更改才能接收 volley 调用。

此外,在调试截击调用时,我注意到 HurlStack.java performRequest 函数下:

int responseCode = connection.getResponseCode();

url 在该行执行后立即从 'connection' 变量中剥离。

此外,如果我将 url 复制粘贴到浏览器中,它会显示我在读取查询参数时期望的数据。

所以,这只发生在 GENYMOTION 模拟器上。当我使用内置模拟器和实际设备时,参数可以正确传递。

在genymotion上发现另一个面临类似情况的人:

因此,任何其他使用 volley 并且碰巧正在使用 genymotion 的人,您可能需要切换模拟器(至少现在)。