本文介绍了HttpResponse 使用 android 问题:执行总是导致异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在从事一个 Android 项目,现在我想向一些 API 询问信息.看起来这应该是非常基本的!

这是我的代码的一般要点:

private InputStream retrieveStream2(String url){DefaultHttpClient 客户端 = 新的 DefaultHttpClient();HttpGet getRequest = new HttpGet(url);System.out.println("getRequest == " + getRequest);尝试 {HttpResponse getResponse = client.execute(getRequest);//这里有问题final int statusCode = getResponse.getStatusLine().getStatusCode();如果(状态码!= HttpStatus.SC_OK){Log.w(getClass().getSimpleName(),"错误" + statusCode + " 为 URL " + url);返回空;}HttpEntity getResponseEntity = getResponse.getEntity();返回 getResponseEntity.getContent();}捕获(例外 e){getRequest.abort();Log.w(getClass().getSimpleName(), "URL 错误, YO " + url, e);}返回空;}

其中 url 变量是字符串http://search.twitter.com/search.json?q=javacodegeeks".

如您所见,该站点上有一些不错的 JSON 信息;我的问题是每次调用 ''client.execute(getRequest);'' 时,程序都会抛出并捕获异常.没用处!

我听说过两件事:

1) 您必须为模拟器/设备设置使用互联网的权限!——我想我已经涵盖了这一点,但也许我做错了!在 androidmanifest.xml 我添加了

就是这样.

2)(我不太确定)您不能在ui"线程中启动网络"线程.我不完全确定这意味着什么,但我继续学习了一些关于 Android 线程、处理程序和 AsyncTasks 的教程.这里:请查看我遵循的 AsyncTask 教程下的代码:

http://www.vogella.de/articles/AndroidPerformance/article.html>

跟着AsyncTask教程,我发现我还是有同样的问题--

行:HttpGet httpGet = 新的 HttpGet(url)总是抛出异常,就像以前一样.

这是我尝试使用上面的线程教程得到的 logcat:

02-27 20:43:28.565: I/ActivityManager(92): 从 pid 574 开始 {cmp=com.Prometheus.R1/.JsonParsingActivity}02-27 20:43:28.565:W/WindowManager(92):将 (180x300) 的屏幕截图截取到 21010 层失败02-27 20:43:28.896:I/System.out(574):预执行02-27 20:43:29.236: I/ActivityManager(92): 显示 com.Prometheus.R1/.JsonParsingActivity: +638ms02-27 20:43:29.329: I/ARMAssembler(35): 生成扫描线__00000077:03010104_00008001_00000000 [89 ipp] (110 ins) 在 [0x40fad4000077:03008001_00000000 [0x40fad4000000] (110 ins) 在 [0x40fad400007700000000000000000000000000000 [0x40fad40000000000 [89 ipp] (110 ins)02-27 20:43:30.016:W/System.err(574):java.net.UnknownHostException:无法解析主机search.twitter.com":没有与主机名关联的地址02-27 20:43:30.016:W/System.err(574):在 java.net.InetAddress.lookupHostByName(InetAddress.java:426)02-27 20:43:30.026:W/System.err(574):在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)02-27 20:43:30.026:W/System.err(574):在 java.net.InetAddress.getAllByName(InetAddress.java:220)02-27 20:43:30.026:W/System.err(574):在 org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)02-27 20:43:30.036:W/System.err(574):在 org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)02-27 20:43:30.036:W/System.err(574):在 org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)02-27 20:43:30.046:W/System.err(574):在 org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)02-27 20:43:30.046:W/System.err(574):在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)02-27 20:43:30.046:W/System.err(574):在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)02-27 20:43:30.055:W/System.err(574):在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)02-27 20:43:30.055:W/System.err(574):在 com.Prometheus.R1.JsonParsingActivity$DownloadWebPageTask.doInBackground(JsonParsingActivity.java:88)02-27 20:43:30.055:W/System.err(574):在 com.Prometheus.R1.JsonParsingActivity$DownloadWebPageTask.doInBackground(JsonParsingActivity.java:1)02-27 20:43:30.055:W/System.err(574):在 android.os.AsyncTask$2.call(AsyncTask.java:264)<br/>02-27 20:43:30.066:W/System.err(574):在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)02-27 20:43:30.066:W/System.err(574):在 java.util.concurrent.FutureTask.run(FutureTask.java:137)<br/>02-27 20:43:30.066:W/System.err(574):在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)02-27 20:43:30.076:W/System.err(574):在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)02-27 20:43:30.076:W/System.err(574):在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)02-27 20:43:30.087:W/System.err(574):在 java.lang.Thread.run(Thread.java:856)02-27 20:43:30.108:W/System.err(574):引起:libcore.io.GaiException:getaddrinfo 失败:EAI_NODATA(没有与主机名关联的地址)02-27 20:43:30.116:W/System.err(574):在 libcore.io.Posix.getaddrinfo(本机方法)02-27 20:43:30.116:W/System.err(574):在 libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)02-27 20:43:30.126:W/System.err(574):在 java.net.InetAddress.lookupHostByName(InetAddress.java:411)02-27 20:43:30.126:W/System.err(574):... 18 更多02-27 20:43:30.136: I/System.out(574): 除了 url http://search.twitter.com/search.json?q=javacodegeeks, ....02-27 20:43:30.136: I/System.out(574): 响应 =


如您所见,异常是 UnknownHostException:
ava.net.UnknownHostException:无法解析主机search.twitter.com":没有与主机名关联的地址"
但我不认为该网站是不可接受的...

谁能告诉我发生了什么事+我需要做什么才能度过难关?

解决方案

检查清单的权限,确保已添加:

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

I've been working on an Android project and I'm at a point where I want to ask some API for information. Seems like this should be very basic!

Here's the general gist of my code:

private InputStream retrieveStream2(String url)
{
    DefaultHttpClient client = new DefaultHttpClient();

    HttpGet getRequest = new HttpGet(url);
    System.out.println("getRequest == " + getRequest);
    try {


        HttpResponse getResponse = client.execute(getRequest);//here is teh problem
        final int statusCode = getResponse.getStatusLine().getStatusCode();


        if (statusCode != HttpStatus.SC_OK)
        {
            Log.w(getClass().getSimpleName(),
                    "Error " + statusCode + " for URL " + url);
            return null;
        }

        HttpEntity getResponseEntity = getResponse.getEntity();
        return getResponseEntity.getContent();

    }
    catch (Exception e)
    {
        getRequest.abort();
        Log.w(getClass().getSimpleName(), "Error for URL, YO " + url, e);
    }
    return null;
}

where url variable is the string "http://search.twitter.com/search.json?q=javacodegeeks".

As you can see there is some nice JSON info at that site; My problem is that every time ''client.execute(getRequest);'' is called, the program throws and catches an exception. Not useful!

I've heard two things:

1) You have to set permission for the emulator/device to use the internet! -- I think I covered this, but maybe I did it wrong! In the androidmanifest.xml I added

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

So there's that.

2) (which I'm not so certain about) you cannot start a 'networking' thread within a 'ui' thread. I'm not entirely sure what this means, but i went ahead and followed some tutorial on Android Threads, Handlers and AsyncTasks. Here: Please check out the code under the AsyncTask tutorial, which I followed:

http://www.vogella.de/articles/AndroidPerformance/article.html

After following along with the AsyncTask tutorial, I found that I still had the same problem--

the line:HttpGet httpGet = new HttpGet(url)always threw an exception, like before.

Here is the logcat from my attempt with the threading tutorial above:

02-27 20:43:28.565: I/ActivityManager(92): START {cmp=com.Prometheus.R1/.JsonParsingActivity} from pid 574
02-27 20:43:28.565: W/WindowManager(92): Failure taking screenshot for (180x300) to layer 21010
02-27 20:43:28.896: I/System.out(574): pre execute
02-27 20:43:29.236: I/ActivityManager(92): Displayed com.Prometheus.R1/.JsonParsingActivity: +638ms
02-27 20:43:29.329: I/ARMAssembler(35): generated scanline__00000077:03010104_00008001_00000000 [ 89 ipp] (110 ins) at [0x40fad6a8:0x40fad860] in 7204915 ns
02-27 20:43:30.016: W/System.err(574): java.net.UnknownHostException: Unable to resolve host "search.twitter.com": No address associated with hostname
02-27 20:43:30.016: W/System.err(574):  at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
02-27 20:43:30.026: W/System.err(574):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
02-27 20:43:30.026: W/System.err(574):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
02-27 20:43:30.026: W/System.err(574):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
02-27 20:43:30.036: W/System.err(574):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-27 20:43:30.036: W/System.err(574):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-27 20:43:30.046: W/System.err(574):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-27 20:43:30.046: W/System.err(574):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-27 20:43:30.046: W/System.err(574):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-27 20:43:30.055: W/System.err(574):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-27 20:43:30.055: W/System.err(574):  at com.Prometheus.R1.JsonParsingActivity$DownloadWebPageTask.doInBackground(JsonParsingActivity.java:88)
02-27 20:43:30.055: W/System.err(574):  at com.Prometheus.R1.JsonParsingActivity$DownloadWebPageTask.doInBackground(JsonParsingActivity.java:1)
02-27 20:43:30.055: W/System.err(574):  at android.os.AsyncTask$2.call(AsyncTask.java:264)<br/>
02-27 20:43:30.066: W/System.err(574):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-27 20:43:30.066: W/System.err(574):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)<br/>
02-27 20:43:30.066: W/System.err(574):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
02-27 20:43:30.076: W/System.err(574):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-27 20:43:30.076: W/System.err(574):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-27 20:43:30.087: W/System.err(574):  at java.lang.Thread.run(Thread.java:856)
02-27 20:43:30.108: W/System.err(574): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
02-27 20:43:30.116: W/System.err(574):  at libcore.io.Posix.getaddrinfo(Native Method)
02-27 20:43:30.116: W/System.err(574):  at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
02-27 20:43:30.126: W/System.err(574):  at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
02-27 20:43:30.126: W/System.err(574):  ... 18 more
02-27 20:43:30.136: I/System.out(574): Except thrown by url http://search.twitter.com/search.json?q=javacodegeeks, .... 
02-27 20:43:30.136: I/System.out(574): response =


The exception is an UnknownHostException as you can see:
"ava.net.UnknownHostException: Unable to resolve host "search.twitter.com": No address associated with hostname"
But I don't think the site is unacceptable...

Can anyone tell me what's going on + what I need to do to get through it?

解决方案

Check the permissions of your manifest, make sure you have added this:

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

这篇关于HttpResponse 使用 android 问题:执行总是导致异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 07:42