android天气预报(二)

android天气预报(二),第1张

概述android天气预报(二) packagecom.example.weather;importjava.io.UnsupportedEncodingException;importjava.net.URLEncoder;importjava.util.ArrayList;importjava.util.List;importcom.example.weather.adapter.WeatherAdapter;importcom.example.weather. androID天气预报(二)

 

package com.example.weather;import java.io.UnsupportedEnCodingException;import java.net.URLEncoder;import java.util.ArrayList;import java.util.List;import com.example.weather.adapter.WeatherAdapter;import com.example.weather.moder.Weather;import com.example.weather.util.httpCallbackListener;import com.example.weather.util.httpUtil;import com.Google.gson.JsonArray;import com.Google.gson.JsonObject;import com.Google.gson.JsonParser;import androID.app.Activity;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.animation.LayoutAnimationController;import androID.vIEw.animation.ScaleAnimation;import androID.Widget.EditText;import androID.Widget.Imagebutton;import androID.Widget.ListVIEw;import androID.Widget.Toast;public class WeatherActivity extends Activity {	private EditText ecCity;	private Imagebutton btnquery;	private ListVIEw lvFutureWeather;	public static final int SHOW_RESPONSE=1;	private List<Weather> data;	private Handler handler=new Handler(){		public voID handleMessage(Message msg){			switch (msg.what) {			case SHOW_RESPONSE:				String response=(String )msg.obj;				if(response!=null){					parseWithJsON(response);					WeatherAdapter weatherAdapter=new WeatherAdapter							(WeatherActivity.this, 									R.layout.activity_weather_Listitem, data);					lvFutureWeather.setAdapter(weatherAdapter);					ScaleAnimation scaleAnimation=new ScaleAnimation(0,1,0,1);					scaleAnimation.setDuration(1000);					LayoutAnimationController animationController  =  new							LayoutAnimationController(									scaleAnimation, 0.6f);					lvFutureWeather.setLayoutAnimation(animationController);				}				break;			default:				break;			}		}		private voID parseWithJsON(String response) {			// Todo auto-generated method stub			data=new ArrayList<Weather>();			JsonParser parser=new JsonParser();//Json解析器			JsonObject obj=(JsonObject) parser.parse(response);			//获取返回状态吗			String resultcode=obj.get("resultcode").getAsstring();			//状态码如果是200说明数据返回成功			if(resultcode!=null&&resultcode.equals("200")){				JsonObject resultObj=obj.get("result").getAsJsonObject();				JsonArray futureWeatherArray=resultObj.get("future").getAsJsonArray();				for(int i=0;i<futureWeatherArray.size();i++){					Weather  weather=new Weather();					JsonObject weatherObject=futureWeatherArray.get(i).getAsJsonObject();					weather.setDayOfWeek(weatherObject.get("week").getAsstring());					weather.setDate(weatherObject.get("date").getAsstring());					weather.setTemperature(weatherObject.get("temperature")							.getAsstring());					weather.setWeather(weatherObject.get("weather")							.getAsstring());					data.add(weather);				}			}		}	};	@OverrIDe	protected voID onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentVIEw(R.layout.activity_weather);		initVIEws();		setListeners();	}	public voID initVIEws(){		ecCity=(EditText) findVIEwByID(R.ID.etCity);		btnquery=(Imagebutton)findVIEwByID(R.ID.btnquery);		lvFutureWeather=(ListVIEw) findVIEwByID(R.ID.lvFutureWeather);		 data=new ArrayList<Weather>();	}	private voID setListeners(){		btnquery.setonClickListener(new OnClickListener() {			public voID onClick(VIEw v) {				// Todo auto-generated method stub				String cityname=ecCity.getText().toString();								try {					if(cityname.equals("")){						cityname = URLEncoder.encode("滨州", "utf-8");						}else					{						cityname = URLEncoder.encode(cityname, "utf-8");					}									} catch (UnsupportedEnCodingException e1) {					e1.printstacktrace();				}				System.out.println("lvFutureWeather="+lvFutureWeather);				Toast.makeText(WeatherActivity.this, "success",						Toast.LENGTH_LONG).show();				String weatherUrl = "http://v.juhe.cn/weather/index?format=2&cityname="+cityname+"&key=adeb6a007b16fb3470e970853ecced3c";				Toast.makeText(WeatherActivity.this, "success"+weatherUrl,						Toast.LENGTH_LONG).show();				httpUtil.sendhttpRequest(weatherUrl, new httpCallbackListener() {					public voID onFinish(String response) {						// Todo auto-generated method stub						Message message=new Message();						message.what=SHOW_RESPONSE;						//将服务器返回的结果存放到Message中						message.obj=response.toString();						handler.sendMessage(message);					}					public voID one rror(Exception e) {						// Todo auto-generated method stub						System.out.println("访问失败");					}				});			}		});	}}


 

点赞收藏分享文章举报

zhupengqq1发布了194 篇原创文章 · 获赞 1 · 访问量 3962私信 关注 总结

以上是内存溢出为你收集整理的android天气预报(二)全部内容,希望文章能够帮你解决android天气预报(二)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065252.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存