服务抛出未知主机异常 Android 应用程序

发布于 2024-11-17 17:26:10 字数 3292 浏览 3 评论 0原文

我正在尝试检查每 15 分钟由警报唤醒的服务中的可变数量的网站。我遇到的问题是,当我启动服务来检查网站时,它给我一个我知道很好的网站的 java.net.UnknownHostException 。也许我不完全理解正在吐出的错误,但我不确定为什么代码会给我这样的问题。

这是堆栈:

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(560):     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)

这是代码:

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

我非常困惑为什么会发生这种情况。我想我对 Http 请求不太熟悉,但我觉得这应该可行。

编辑*****:我发誓我已经添加了使用互联网权限,但我实际上并没有添加它。现在我知道当我将来遇到 UnknownHostException 时要进行三次检查。

对所有花时间回答的人表示支持,并感谢您招待我的白痴。

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(560):     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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-11-24 17:26:10

可能是因为您的清单中没有互联网访问权限?

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

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

<uses-permission android:name="android.permission.INTERNET" /> 
夏末的微笑 2024-11-24 17:26:10

WiFi 或网络层可能会进入睡眠状态:当服务从警报中唤醒时,它可能需要等待几秒钟,直到网络层恢复并重新建立通信,然后再尝试 HTTP 请求。

编辑:或者,您可能需要使用唤醒锁来获取对 wifi 的访问权限并防止其进入睡眠状态。

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.

征﹌骨岁月お 2024-11-24 17:26:10

如果前面的答案都没有修复它,那么您是否在模拟器中运行它?模拟器中的网络层喜欢偶尔抛出这些错误来让您感到困惑/沮丧。厚颜无耻,就是那个。重新启动它,一切都会恢复正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文