无法连接android系统中使用的HttpConnection [英] Cannot connect using httpconnection in android

查看:166
本文介绍了无法连接android系统中使用的HttpConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Andr​​oid 2.3.3,我做了一个RSS阅读器这是伟大的工作,然后我整合了简单的RSS阅读器的code到另一个活动,只需复制粘贴仔细,没有错误出现。

现在的问题是,当我跑在我的模拟器的应用程序,它给了我错误的连接异常。然后,我想通了,通过把祝酒everyline在try块,这个问题是 httpconnection.connect()之后;

我已经加入了Android清单的权限,而我的logcat给 javaio异常警告:错误连接

 尝试{
        HttpURLConnection的HttpURLConnection的=(HttpURLConnection类)连接;
        //Toast.makeText(this,URLConnection的传递,Toast.LENGTH_SHORT).show();
        httpURLConnection.setAllowUserInteraction(假);
        httpURLConnection.setInstanceFollowRedirects(真正的);
        //Toast.makeText(this,设置响应法,Toast.LENGTH_SHORT).show();
        httpURLConnection.setRequestMethod(GET);
        //Toast.makeText(this,连接,Toast.LENGTH_SHORT).show();
        httpURLConnection.connect();
        Toast.makeText(这一点,在应对code,Toast.LENGTH_SHORT).show();
        响应= httpURLConnection.getResponse code();
        //Toast.makeText(this,响应code通过,Toast.LENGTH_SHORT).show();
        如果(响应== HttpURLConnection.HTTP_OK){
            //Toast.makeText(this,OK,Toast.LENGTH_SHORT).show();
            的InputStream = httpURLConnection.getInputStream();
        }

    }赶上(例外五){
        // TODO:处理异常
        抛出新的IOException异常(错误连接);
    }
 

下面的code的创建对象:

 网​​址URL =新的URL(UrlString);
URLConnection的连接= url.openConnection();
如果(!(的instanceof的HttpURLConnection连接))
  抛出新的IOException异常(不是HTTP连接);
 

解决方案
  

它给networkonmainthread异常

这意味着,你正在做的网络操作的主线程上,而这不是在严格模式允许的。

在主线程通常意味着你正在做的事件处理程序,如 onResume 的onPause ,或在您的OnClickListener的的onClick 方法。你可以看看这个帖子(无痛线程),以了解发生了什么问题以及为什么这是不允许的。通常,如果在主线程执行网络操作,这将阻止GUI事件的处理,并且如果网络操作是缓慢的,则应用程序将是没有反应。

出现在API级别9严格模式,所以这个解释说,为什么你有问题,如果分SDK版本为10,为什么它的工作原理与分SDK版本8。

您应该使用 AsyncTask的或其他方法来做到这一点的背景。你可以参考 1 这一点。

I am using android 2.3.3, i had made an rss reader which was working great, then i integrated the code of that simple rss reader into another activity, just copy pasted it carefully, no errors there.

The problem is when i run the app on my emulator it gives me error connecting exception. Then i figured out by putting toasts after everyline in try block that the problem is at httpconnection.connect(); line.

I have added the permission in the android manifest while my logcat gives a warning of javaio exception:error connecting.

    try {
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
        //Toast.makeText(this, "urlconnection passed", Toast.LENGTH_SHORT).show();
        httpURLConnection.setAllowUserInteraction(false);
        httpURLConnection.setInstanceFollowRedirects(true);
        //Toast.makeText(this, "setting response method", Toast.LENGTH_SHORT).show();
        httpURLConnection.setRequestMethod("GET");
        //Toast.makeText(this, "connecting", Toast.LENGTH_SHORT).show();
        httpURLConnection.connect();
        Toast.makeText(this, "on response code", Toast.LENGTH_SHORT).show();
        response = httpURLConnection.getResponseCode();
        //Toast.makeText(this, "response code passed", Toast.LENGTH_SHORT).show();
        if (response == HttpURLConnection.HTTP_OK) {
            //Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
            inputStream = httpURLConnection.getInputStream();
        }

    } catch (Exception e) {
        // TODO: handle exception
        throw new IOException("Error connecting");
    }

Here the code which creates the object:

URL url = new URL(UrlString);
URLConnection connection = url.openConnection();
if (!(connection instanceof HttpURLConnection)) 
  throw new IOException("Not a HTTP connection"); 

解决方案

It gives networkonmainthread exception

This means that you are doing network operations on the main thread, and that's not allowed in the Strict Mode.

"In the main thread" usually means that you are doing it in the event handlers, like onResume, onPause, or in the onClick method of your OnClickListener. You may look at this post (Painless threading) to understand what's the problem and why this is not allowed. Generally, if you perform network operations in the main thread, it would block the processing of GUI events, and if the network operation was slow, your application would be not responsive.

Strict Mode appeared in API level 9, so this explains that why you have problem if min SDK version is 10, and why it works with min SDK version 8.

You should use AsyncTask or other methods to do it in the background. You may refer to 1 for this.

这篇关于无法连接android系统中使用的HttpConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆