Android可以获取google.com,但不能访问任何其他网页

时间:2016-02-14 00:34:38

标签: android android-asynctask android-async-http

我正在使用android-async-http来运行简单的推送通知示例。 该设备能够很好地接收通知。收到文件后,我使用上面提到的库调用get请求:

Log.d("TAG", "GOT");
        AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://mysafeinfo.com/api/data?list=states&format=json", new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                // called before request is started
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                // called when response HTTP status is "200 OK"
                try {
                    String decoded = new String(response, "UTF-8");
                    Log.d("TAG", decoded);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                // called when response HTTP status is "4XX" (eg. 401, 403, 404)

                try {
                    String dec = new String(errorResponse, "UTF-8");
                    Log.d("TAG",dec);
                }
                catch (UnsupportedEncodingException q){
                    q.printStackTrace();

                }
            }

            @Override
            public void onRetry(int retryNo) {
                // called when request is retried
            }
        });

在那个url应该返回json,但在我的日志中,我根本看不到任何消息。它全部空白,从不调用log.d。但是当我将该网址更改为www.google.com时,它会返回一些HTML(这是谷歌的主页)。 我添加了

<uses-permission android:name="android.permission.INTERNET" />

到清单。

这是我的logcat:

  

02-13 21:06:12.189   12101-12101 / com.example.harshvardhangupta.gcmpleasework D / TAG:GOT   02-13 21:06:12.468   12101-12101 / com.example.harshvardhangupta.gcmpleasework V / AsyncHttpRH:   进展1310自1522(86%)02-13 21:06:12.468   12101-12101 / com.example.harshvardhangupta.gcmpleasework V / AsyncHttpRH:   进展3333自1522(219%)02-13 21:06:12.469   12101-12101 / com.example.harshvardhangupta.gcmpleasework V / AsyncHttpRH:   进展5238从1522(344%)

请注意虚线。在@Taylor Courtney的评论之后,我注意到响应即将到来,但它没有正确地从字节数组转换为字符串

1 个答案:

答案 0 :(得分:1)

好吧,您的AsyncHttpClient(doc)正在请求返回JsonArray(response)的链接。你应该使用JsonHttpResponseHandler(doc)代替。

AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://mysafeinfo.com/api/data?list=states&format=json", new JsonHttpResponseHandler("UTF-8") {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                super.onSuccess(statusCode, headers, response);
                Log.d(TAG, "onSuccess response: " + response.toString());
                }
            }
        });