HTT presponse采用了android问题:执行总是会引起异常?采用了、异常、问题、HTT

2023-09-05 02:05:05 作者:胖狗

我一直在Android项目,我在一个点,我要问的一些信息的API。看起来这应该是很基本的!

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!

下面是我的code的一般要点是:

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

其中URL变量是字符串http://search.twitter.com/search.json?q=java$c$cgeeks。

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

正如你可以看到有在该网站一些不错的JSON信息;我的问题是,每一次'client.execute(调用getRequest);'被调用时,程序会抛出和捕获异常。没有用!

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!

我听到两件事情:

1)必须设置权限模拟器/设备使用互联网!    - 我想我介绍了这一点,但也许我没有错!   在AndroidManifest.xml我加了

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),(这我不太确定有关),你不能在一个UI线程启动联网主题。我不完全知道这意味着什么,但我说干就干,随后在Android的线程,处理程序和AsyncTasks一些教程。这里:

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

在随着AsyncTask的教程下面,我发现,我仍然有同样的问题 -

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

行: HTTPGET HTTPGET =新HTTPGET(URL) 总是抛出一个异常,像以前一样。

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

下面是我的尝试与线程教程上面的logcat的:

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 =

唯一的例外是一个UnknownHostException,你可以看到: ava.net.UnknownHostException:无法解析主机search.twitter.com:与主机名无地址相关联 但我不认为该网站是不可接受的......

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?

推荐答案

想通了。显然,我把我的AVD关闭的飞行模式。

Figured it out. Apparently I turned my AVD off of "airplane mode".

对于有任何人同样的问题,因为我,也许你正在使用无线网络。出于某种原因,安卓看你的局域网卡,所以转到您的网络连接,并右键单击局域网上card--禁用的事!问题解决了。

For anybody having the same problem as me, you're probably using wireless. For some reason the andriod looks at your LAN card, so go to your Network Connections and right click on the LAN card-- DISABLE THAT THING! Problem solved.

 
精彩推荐
图片推荐