在android中获取http响应的替代方法

时间:2011-04-17 18:00:30

标签: android http

我用

得到空响应
HttpResponse response = client.execute(post);

此方法有时会给出正确的响应,但主要是在没有任何代码更改的情况下给出null响应。我也尝试使用AsyncTask,但行为相同。以下是AsyncTask的代码。如有任何建议请告诉我。 谢谢。

protected void onPreExecute() {
     client = new DefaultHttpClient();
     HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT_MS);
     HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT_MS);
     post = new HttpPost("http://www.google.co.in/m");
     pairs = new ArrayList<NameValuePair>();
     pairs.add(new BasicNameValuePair("hl", "en"));
     pairs.add(new BasicNameValuePair("gl", "us"));
     pairs.add(new BasicNameValuePair("source", "android-launcher-widget"));
     pairs.add(new BasicNameValuePair("q", "persistent"));
     try {
            post.setEntity(new UrlEncodedFormEntity(pairs));
            SystemClock.sleep(400);

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    this.dialog.setMessage("starts...");
    this.dialog.show();

}
@Override
protected HttpResponse doInBackground(Void... arg0) {
    try {
        HttpResponse response = client.execute(post); //this returns null responce
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
        {
             return response;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        //Toast.makeText(url.this, "problem in execute....", 20).show();
        // TODO Auto-generated catch block
        //put the balance is empty dialog box here.
        e.printStackTrace();
    }catch(NullPointerException e)
    {
        e.printStackTrace();
    }

    return null;
}

我的日志说..

04-20 20:19:55.877: WARN/System.err(365): java.net.UnknownHostException: www.google.co.in
04-20 20:19:55.897: WARN/System.err(365): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)

04-20 20:19:55.897: WARN/System.err(365): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)

04-20 20:19:55.907: WARN/System.err(365): at java.net.InetAddress.getAllByName(InetAddress.java:256)

04-20 20:19:55.907: WARN/System.err(365): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)

04-20 20:19:55.907: WARN/System.err(365): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)

04-20 20:19:55.927: WARN/System.err(365): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-20 20:19:55.927: WARN/System.err(365): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)

04-20 20:19:55.937: WARN/System.err(365): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

04-20 20:19:55.937: WARN/System.err(365): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

04-20 20:19:55.947: WARN/System.err(365): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

04-20 20:19:55.947: WARN/System.err(365): at comm.sample.Urlasync$AddStringTask.doInBackground(Urlasync.java:82)

04-20 20:19:55.947: WARN/System.err(365): at comm.sample.Urlasync$AddStringTask.doInBackground(Urlasync.java:1)

04-20 20:19:55.947: WARN/System.err(365): at android.os.AsyncTask$2.call(AsyncTask.java:185)

04-20 20:19:55.957: WARN/System.err(365): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

04-20 20:19:55.957: WARN/System.err(365): at java.util.concurrent.FutureTask.run(FutureTask.java:138)

04-20 20:19:55.957: WARN/System.err(365): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)

04-20 20:19:55.967: WARN/System.err(365): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)

04-20 20:19:55.967: WARN/System.err(365): at java.lang.Thread.run(Thread.java:1019)

1 个答案:

答案 0 :(得分:1)

几天前我遇到了同样的问题。在自己的线程上运行HttpResponse修复了Null响应。尝试使用java线程而不是android异步任务。 http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html