Android activity 中 打开Url 网络地址报错

public void dialog(View view)
{
String msg="";
Toast toast=Toast.makeText(this, "", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);

    LinearLayout layout=(LinearLayout)toast.getView();
    layout.setMinimumHeight(50);
    ImageView image=new ImageView(getApplicationContext());
    image.setImageResource(R.drawable.ic_launcher);
    image.setMaxHeight(50);
    image.setMaxWidth(50);
    layout.addView(image, 0);
    TextView txt=new TextView(getApplicationContext());

    try
    {

        String url_1="http://192.168.43.1:8080/android/legendary.db",url_2="http://localhost:8080/android/legendary.db";
        URI uri=new URI(url_2);
        URL url=uri.toURL(); 
        url.openConnection();
        InputStream in=url.openStream();
        toast.setText("---->");
        txt.setText("\n-----****");
        txt.setTextColor(Color.parseColor("#ffaaff"));
    }
    catch (Exception e)
    {
        msg = e.toString();
        e.printStackTrace();
        txt.setText(e.toString() + "\n-----****");
        txt.setTextColor(Color.RED);
        txt.setTextSize(16);

    }
    layout.addView(txt, 1);
    toast.show();

latelog2(msg);
setlog2();
notice("错误---->", msg);

}

/*错误信息
android.os.NetworkOnMainThreadException
*/

错误信息是不能在主线程中进行网络相关的操作。应该用thread+handler或者asynctask处理。

http://localhost:8080/android/legendary.db
难道你的android客户端上还有web服务器?

我也是个菜鸟,刚昨天遇到这个问题,就是activity里面不让连接网络,怎么解决你去查一下

 android.os.NetworkOnMainThreadException 是主线程联网异常,主线程是不能进行联网,查数据等耗时操作的.在主线程进行耗时操作会导致ui不流程
    ,可开子线程获取数据后通过handle发送消息在主线程序中刷新UI

谢谢大家,现在懂了。。。。。