waether

来源:互联网 发布:个人征信所用数据 编辑:程序博客网 时间:2024/06/03 13:53
public class WeatherActivity extends Activity{private EditText etCity;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(android.os.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);}default:break;}}private void parseWithJSON(String response){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);}}}};protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_weather);initViews();setListeners();}private void initViews(){etCity = (EditText) findViewById(R.id.etCity);btnQuery = (ImageButton) findViewById(R.id.btnQuery);lvFutureWeather = (ListView) findViewById(R.id.lvFutureWeather);}private void setListeners(){btnQuery.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View view){String city = etCity.getText().toString();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="+ city + "&key=818a41d8bda422cb562752959bb72733";HttpUtil.sendHttpRequest(weatherUrl, new HttpCallbackListener(){public void onFinish(String response){Message message = new Message();message.what = SHOW_RESPONSE;// 将服务器返回的结果存放到 Message 中message.obj = response.toString();handler.sendMessage(message);}public void onError(Exception e){System.out.println("访问失败");}});}});}public boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.weather, menu);return true;}

0 0
原创粉丝点击