android-async-http总是onFailure(状态码= 0),但DefaultHttpClient没问题

时间:2015-10-12 02:39:18

标签: android asynchronous httprequest androidhttpclient

我尝试使用android-async-http,但总是onFailure,从日志中我可以看到状态代码= 0.但是,当我使用传统方式时(org.apache.http.client.HttpClient) ,状态代码= 200;

代码:

公共类HttpTestActivity扩展了ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_http_test);

    testHttp();
    testHttp2();
}

public void testHttp2() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet("http://www.baidu.com");
            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                Log.i("ws", "---->>22: status code: " + statusCode);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

public void testHttp() {
    Log.i("ws", "---->>testHttp");
    VStarRestClient.get("http://www.baidu.com", null, new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            Log.i("ws", "---->>onSuccess :" + statusCode);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            Log.i("ws", "---->>onFailure : " + statusCode);
        }
        });
}

1 个答案:

答案 0 :(得分:0)

playerObject

如果错误为0意味着,请检查错误 - 字段, 它显示确切的问题,如java.net.SocketTimeoutException等