android错误之android.os.NetworkOnMainThreadException

在做一个天气预报的widget的时候,参考了一个源代码,但是一直报错,就从里面抠出来获取天气的代码试试看,结果总是报错 就是这个异常,android.os.NetworkOnMainThreadException代码是这样的:MainActivity:public class MainActivity extends Activity { MyWeather myWe

大家好,又见面了,我是全栈君。

在做一个天气预报的widget的时候,参考了一个源代码,但是一直报错,就从里面抠出来获取天气的代码试试看,结果总是报错

 android错误之android.os.NetworkOnMainThreadException

就是这个异常,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(转载请说明出处)

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/121392.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • oracle导出dmp文件命令_linux强制命令

    oracle导出dmp文件命令_linux强制命令SRVCTL是ORACLE9iRAC集群配置管理的工具。本文是对SRVCTL的所有命令进行详细说明的一篇参考文档。读者对象:ORACLE9iRAC数据库集群管理员。注:RAC:RealApplicationClustersSRVM:ServerManagementSRVCTLAdd添加数据库或实例的配置信息。在增加实例中,与-i一起指定的名字应该与INSTANCE_NAME和 ORAC…

    2022年9月3日
    4
  • 静态局部变量和静态全程变量static。

    静态局部变量和静态全程变量static。1.什么是static?static是C/C++中很常用的修饰符,它被用来控制变量的存储方式和可见性。1.1static的引入我们知道在函数内部定义的变量,当程序执行到它的定义处时,编译器为它在栈上分配空间,函数在栈上分配的空间在此函数执行结束时会释放掉,这样就产生了一个问题:如果想将函数中此变量的值保存至下一次调用时,如何实现?最容易想到的方法是定义为全局的变量,但定…

    2022年5月1日
    36
  • Android 跳转到安卓市场进行下载,跳转至应用商店下载

    Android 跳转到安卓市场进行下载,跳转至应用商店下载

    2021年9月30日
    488
  • fseek函数用法_fwrite函数的用法

    fseek函数用法_fwrite函数的用法转载请注明出处:https://blog.csdn.net/wl_soft50/article/details/7787521每天进步一点点–>函数fseek()用法在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅。函数功能是把文件指针指向文件的开头,需要包含头文件stdio.hfseek函数名:fseek功能:重定位流上的文件…

    2022年10月21日
    0
  • 第二章:activiti工作流连接数据库,和eclipse安装activiti插件

    第二章:activiti工作流连接数据库,和eclipse安装activiti插件第二章:activiti工作流连接数据库,和eclipse安装activiti插件

    2022年4月23日
    52
  • MATLAB 数学应用 微分方程 时滞微分方程 具有常时滞的DDE「建议收藏」

    MATLAB 数学应用 微分方程 时滞微分方程 具有常时滞的DDE「建议收藏」本文讲述了如何使用dde23对具有常时滞的DDE(时滞微分方程)方程组求解。方程组为:y1′(t)=y1(t−1)y’_1(t)=y_1(t−1)y1′​(t)=y1​(t−1)y2′(t)=y1(t−1)+y2(t−0.2)y’_2(t)=y_1(t-1)+y_2(t-0.2)y2′​(t)=y1​(t−1)+y2​(t−0.2)y3′(t)=y2(t)y’_3(t)=y_2(t)y3′​(t)=y2​(t).t≤0的历史解函数是常量y1(t)=y2(t)=y3(t)=1y_1(t)=y

    2022年9月28日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号