我正在使用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
}
});
在该网址中应返回json,但在我的日志中,我完全看不到任何消息。它全部为空,从不调用该log.d。但是,当我将该网址更改为www.google.com时,它将返回一些html(这是google的主页)。
我已经添加了
<uses-permission android:name="android.permission.INTERNET" />
到清单。
这是我的日志:
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.gcm请执行V / AsyncHttpRH:
从1522获得1310(86%)02-13 21:06:12.468
12101-12101 / com.example.harshvardhangupta.gcm请执行V / AsyncHttpRH:
从1522获得3333(219%)02-13 21:06:12.469
12101-12101 / com.example.harshvardhangupta.gcm请执行V / AsyncHttpRH:
从1522获得5238(344%)
注意虚线。 @Taylor Courtney发表评论后,我注意到响应即将到来,但响应未正确从字节数组转换为字符串
最佳答案
好吧,您的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());
}
}
});