开发者

Service throwing an Unknown Host Exception Android App

I am trying to check a variable amount of websites in a service that is woken up by an alarm every 15 minutes. The problem I'm having is when I start the service to check the websites it is giving me an java.net.UnknownHostException for a website I know is good. Perhpas I don't fully understand the error that is being spit out, but I'm not sure why the code is giving me such a problem.

Here is the stack:

06-28 19:30:06.270: WARN/System.err(560): java.net.UnknownHostException: http://www.cnn.com/
06-28 19:30:06.480: WARN/System.err(560):     at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
06-28 19:30:06.480: WARN/System.err(560):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:280)
06-28 19:30:06.490: WARN/System.err(560):     at java.net.InetAddress.getByName(InetAddress.java:310)
06-28 19:30:06.520: WARN/System.err(560):     at com.myapp.tracker.Listen.checkWebsite(Listen.java:102)
06-28 19:30:06.520: WARN/System.err(560):     at com.myapp.tracker.Listen.onStartCommand(Listen.java:64)
06-28 19:30:06.520: WARN/System.err(560):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
06-28 19:30:06.530: WARN/System.err(560):     at android.app.ActivityThread.access$3600(ActivityThread.java:125)
06-28 19:30:06.530: WARN/System.err(560):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
06-28 19:30:06.530: WARN/System.err(560):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 19:30:06.530: WARN/System.err(560):     at android.os.Looper.loop(Looper.java:123)
06-28 19:30:06.550: WARN/System.err(560):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 19:30:06.550: WARN/System.err(56开发者_JAVA百科0):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 19:30:06.550: WARN/System.err(560):     at java.lang.reflect.Method.invoke(Method.java:521)
06-28 19:30:06.550: WARN/System.err(560):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 19:30:06.550: WARN/System.err(560):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 19:30:06.550: WARN/System.err(560):     at dalvik.system.NativeStart.main(Native Method)

Here is the code:

public void checkWebsite(Intent intent) throws Exception {
    BufferedReader in = null;
    try {
        Bundle bundle = intent.getExtras();
        int [] checkboxes = bundle.getIntArray("checkboxes");
        if( checkboxes[0] == 0){
            String failure = "******FAILURE VALUE = 0";
            Log.d(TRACKER_DEBUGGING, failure);  
        }
        String [] websites = bundle.getStringArray("websites");
        if( websites[0] == null){
            String failure = "******FAILURE VALUE = null";
            Log.d(TRACKER_DEBUGGING, failure);  
        }
        for(int i = 0; i < 3; i++ ){
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();

            if( checkboxes[i] == 1 ){
                request.setURI(new URI(websites[i]));
                Log.d(TRACKER_DEBUGGING, websites[i]);

                // this line right here is throwing the error
                HttpResponse response = client.execute(request);

// more code after this but it never reaches this point or executes any of it

I'm super perplexed as to why this is happening. I guess I'm not really all that familiar with Http requests, but I feel like this should work.

EDIT*****: I swore that I had added the uses internet permission but I had not actually added it. Now I know to triple check for that when I run into an UnknownHostException in the future.

Upvotes for all that took the time to answer and thank you for entertaining my idiocy.


Might be because you do not have Internet access in the manifest?

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


The WiFi or networking layer might be going to sleep: when the service wakes up from the alarm it may need to wait a few seconds until the networking layer comes back up and re-establishes communication before trying the HTTP request.

EDIT: alternatively you may need to use a wake lock to grab access to the wifi and prevent it from going to sleep.


If none of the previous answers fixed it, then, are you running this in an emulator? The network layer in the emulator likes to throw these errors every once in a while to confuse/frustrate you. Cheeky, that one. Restart it and everything should be back to normal.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜