开发者

alternative method for getting http response in android

开发者 https://www.devze.com 2023-02-27 03:00 出处:网络
I get the null response with HttpResponse response = client.execute(post); This method some time give correct response but mostly give the null response without any code change. I also tried it us

I get the null response with

HttpResponse response = client.execute(post);

This method some time give correct response but mostly give the null response without any code change. I also tried it using the AsyncTask, but got same behavior. Following is my code with AsyncTask. If any sugge开发者_如何学编程stion please tell me. Thank you.

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;
}

my log says..

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)


I ran into this same problem a few days ago. Running the HttpResponse on its own thread fixed the Null response. Try using java threading instead of the android async task. http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html

0

精彩评论

暂无评论...
验证码 换一张
取 消