初学安卓网络这一块 ,很基础,大家别见笑程序很简单,访问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.NetworkOnMainThreadExceptionat 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)TomcatURLAndroid浏览器
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.NetworkOnMainThreadExceptionat 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)TomcatURLAndroid浏览器
解决方案 »
- android新手求救啊,自学太痛了,求各位牛人指点啊 错误在最下面
- android 纹理贴图 模拟器上正常 为什么真机上就是空白
- 请问,如何将(当前桌面上)gridview中的内容映射成图片
- 冰山上的提问:WebChromeClient的onReceivedIcon(WebView view, Bitmap icon)方法
- android 环境变量怎么设置?
- Android Matrix
- Android SDK更新错误,求助
- viewpage在listview中第一行,导致viewpage不能左右滑动,要如何解决呢?
- unfortunately MyFirst has stopped 如何解决
- 家里的网没法上CSDN的博客
- 强大的Andriod教程下载(10套)
- Android客户端webservice服务端sql数据库根据Title查询信息
我用的是安卓应用开发揭秘上面的例子