访问TOMCAT上一个文件老是报错,大家伙看看。。。

月出惊弓鸟 月出惊弓鸟 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
初学安卓网络这一块 ,很基础,大家别见笑

程序很简单,访问TOMCAT 下面的一个TXT文件 在浏览器上面已经成功访问到了 但程序一直报错退出
public class Activity01 extends Activity {
private TextView httpText;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.http1);
httpText = (TextView)findViewById(R.id.httpText);
String httpUrl = "http://192.168.5.103:8080/jsp/http.txt";
String data = "";
URL url =null;
try{
url = new URL(httpUrl);
System.out.println(url+"");
}
catch(MalformedURLException e){
Log.v("Activity01","MalformedURLException");
}
if(url!=null){
try{
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
BufferedReader br = new BufferedReader(in);
String inputLine = null;
while((inputLine=br.readLine())!=null){
data+=inputLine + "\n";
}
in.close();
urlConn.disconnect();
httpText.setText(data);
}
catch(IOException e){
Log.v("Activity01","IOException");
}
}
else{httpText.setText("内容为空");}
}

}


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

错误LOG是 :java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test_http1/com.example.test_http1.Activity01}: android.os.NetworkOnMainThreadException

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
月出惊弓鸟
Android 80,490 成员 91,371 内容