在做一个天气预报的widget的时候,参考了一个源代码,但是一直报错,就从里面抠出来获取天气的代码试试看,结果总是报错
就是这个异常,android.os.NetworkOnMainThreadException
代码是这样的:
MainActivity:
public class MainActivity extends Activity {
MyWeather myWeather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWeather = new MyWeather();
System.out.println(myWeather.getCity());
}
}
MyWeather:
public class MyWeather {
public String city;
public String temp1;
public String weather1;
public String img1;
public MyWeather(){
getWeather();
}
public void getWeather(){
try {
URL url = new URL("http://m.weather.com.cn/data/101250101.html");
InputStream is = url.openStream();
int len = -1;
byte[] buffer = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
String info = bos.toString("utf-8");
JSONObject dataJson = new JSONObject(info);
JSONObject json = dataJson.getJSONObject("weatherinfo");
city = json.getString("city");
temp1 = json.getString("temp1");
weather1 = json.getString("weather1");
img1 = json.getString("img1");
bos.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (JSONException e) {
// TODO: handle exception
}
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTemp1() {
return temp1;
}
public void setTemp1(String temp1) {
this.temp1 = temp1;
}
public String getWeather1() {
return weather1;
}
public void setWeather1(String weather1) {
this.weather1 = weather1;
}
public String getImg1() {
return img1;
}
public void setImg1(String img1) {
this.img1 = img1;
}
}
经过查阅资料,大体错误应该是这样的,主要是说主线程访问网络时出的异常。
Android在4.0之前的版本 支持在主线程中访问网络,但是在4.0以后对这部分程序进行了优化,也就是说访问网络的代码不能写在主线程中了。
于是把MainActivity的代码改成如下就可以了,在新开的线程中读取天气,用handler异步加载:
public class MainActivity extends Activity {
MyWeather myWeather;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
System.out.println("jason.com" + myWeather.getCity());
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
myWeather = new MyWeather();
Message msg = handler.obtainMessage();
msg.what = 0;
handler.sendMessage(msg);
}
}.start();
}
}
作者:jason0539
微博:http://weibo.com/2553717707
博客:http://blog.csdn.net/jason0539(转载请说明出处)
分享到:
相关推荐
主要介绍了Caused by: android.os.NetworkOnMainThreadException错误解决办法,本文提供了2种解决方法,需要的朋友可以参考下
在Android中,如果想退出Android程序,一般都是调用finish()、System.exit(0)android.os.Process.killProcess(android.os.Process.myPid())等方法来实现退出程序功能,可是在实际开发中,并不能达到完全退出应用程序...
android.os.NetworkOnMainThreadException 解决事情应该优先解决一件事情,总是三心二意,最后塞翁失马 文章目录android.os.NetworkOnMainThreadException问题分析查阅资料解决方案知识要点: RunOnUiThread 和 new...
android viewpager android-support-v4.jar
这是最新版的android-support-v4.jar,大小1MB多,比其它那种几百K的要大,因为这是最新的android-support-v4.jar,有最新的android.support.v4.view.animation类,现在是2015年10月,这是截至目前最新的jar包
This is a development tool to show values of android.os.Build.* Try it Installing android SDK See the official android doc Create emulator The command line for creating an AVD has the following syntax...
Android Studio Hedgehog 2023.1.1 Patch 2(android-studio-2023.1.1.28-cros.deb)适用于Chrome OS系统。
My goal is to make your life easier, and, at the same time, help you learn about the Android OS. The kitchen will not automatically turn you into a developer. You are not programming anything or ...
或许困扰很多Android开发者最大的问题莫过于Bug的修改了,调Bug 改Bug,想想都头疼,以下就是一位对Bug很有经验的开发者总结出来的有关Android错误的解决方法。
Apress.Pro.Android.Web.Apps.Developing.HTML5.JavaScript.CSS.and.Chrome.OS.Web.Apps
Android Studio Bumblebee 2021.1.1 Patch 3 (android-studio-2021.1.1.23-cros.deb)大黄蜂,适用于Chrome OS系统,是Android Studio 4.3(Android Studio Arctic Fox 2020.3.1.26新版本命名)版本之后的新版本...
Android webview 遇到android.os.FileUriExposedException错误解决办法 1. 在 Manifest 文件中添加: <provider android:name=android.support.v4.content.FileProvider android:authorities=${...
谷歌新推出的复合MaterialDesign设计规范的包叫android.support.design.widget 。这是其中TabLayout的典型用法
Android Studio 4.3 (Android Studio Arctic Fox 2020.3.1.25)开发工具版本适用Chrome OS系统,android-studio-2020.3.1.25-cros.deb,它是Android Studio 4.2.2的下一个版本Android Studio 4.3版本(Android Studio ...
MT6589_Android_scatter_emmc.txt 配置文件
Android Studio 4.3 (Android Studio Arctic Fox 2020.3.1.26)开发工具版本适用Chrome OS系统,android-studio-2020.3.1.26-cros.deb,它是Android Studio 4.2.2的下一个版本Android Studio 4.3版本(Android Studio ...
使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚.Google在API 21中提供了通用的解决方案,那就是android-support-multidex.jar. 这个jar包最低可以支持到API 4的版本(Android L及以上...
最近在Android N 上 安装Apk时报错:android.os.FileUriExposedException: file:///storage/emulated/0/Download/appName-2.3.0.apk exposed beyond app through Intent.getData(),通过查找相关的资料终于找到了...