简体   繁体   English

Android Volley Request使用参数使用JSON API到Wordpress

[英]Android Volley Request using parameters to Wordpress using JSON API

I currently use HttpClient/HttpResponse calls in my android app to transfer and receive data to my wordpress site and it works just fine. 我目前在我的android应用程序中使用HttpClient / HttpResponse调用来向我的wordpress网站传输和接收数据,并且效果很好。 I use https://wordpress.org/plugins/json-api/ plugin on the site to handle the requests. 我在网站上使用https://wordpress.org/plugins/json-api/插件来处理请求。 Google is trying to phase out those http protocols and suggests using volley for networking instead. Google正在尝试逐步淘汰这些http协议,并建议改用Volley进行联网。 So i'm trying to make the transition but the query post parameters to the server gets rejected somehow. 所以我正在尝试进行转换,但是以某种方式拒绝了服务器的查询后参数。

Using code from here as a starting point: http://code.tutsplus.com/tutorials/creating-a-weather-application-for-mars-using-volley--cms-23812 使用此处的代码作为起点: 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);
}

On my server php code, i'm printing the $_SERVER(the $_POST and $_GET are empty) global variable to see incoming requests and shows the following(only showing relevant entries in the array): 在我的服务器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' => ''
    )

Notice the Request_uri is stripped out of the query parameters and i'm not able to get the right post that i need. 请注意,Request_uri已从查询参数中删除,并且我无法获得所需的正确信息。 I've tried debugging the volley code quite a bit and read through a lot of SO posts on passing the right json data the right way through volley, but none of the approaches works. 我已经尝试了很多调试齐射代码,并在通过齐射正确方式传递正确的json数据时通读了很多SO文章,但是这些方法均无效。 The error it appears seems to be somewhere on the server side as, if i use a different api url (flickr for example), i'm able to make successful calls. 出现的错误似乎在服务器端,因为如果我使用其他api网址(例如,flickr),则可以进行成功调用。 What changes do i need to make to my server to recieve volley calls as compared to the httpclient calls. 与httpclient调用相比,我需要对服务器进行哪些更改以接收凌空调用。

Also, while debugging the volley calls, i noticed under HurlStack.java performRequest function: 另外,在调试齐射调用时,我注意到在HurlStack.java performRequest函数下:

int responseCode = connection.getResponseCode();

The url is stripped out at this point from the 'connection' variable right after the line is executed. 执行该行后,此时从“连接”变量中删除网址。

Also, if i copy paste the url into a browser, it shows me the data i'm expecting as it reads the query parameters. 另外,如果我将网址复制粘贴到浏览器中,它会显示读取查询参数时所期望的数据。

So, this was only happening on GENYMOTION emulator. 因此,这仅在GENYMOTION模拟器上发生。 The parameters get passed correctly when i use the built in emulator and an actual device. 当我使用内置仿真器和实际设备时,参数会正确传递。

Found another person facing a similar situation on genymotion: Volley request not taking parameters 发现另一个在genymotion上面临类似情况的人: Volley请求不接受参数

So, anyone else using volley and happened to be using genymotion, you may have to switch your emulators (for now at least). 因此,其他任何使用齐射并碰巧正在使用genymotion的人,都可能不得不切换模拟器(至少现在是这样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM