做了个android天气预报运行实现不了,求大神帮忙

邸咪 发布于 2014/08/21 16:41
阅读 314
收藏 0

package com.example.weather;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Xml;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class MainActivity extends Activity {
 protected static final int SUCCESS = 1;
 protected static final int ERROR = 2;
 private EditText et_cityname;
 private TextView tv1_info;
 private TextView tv2_info;
 private TextView tv3_info;
 private String basePath = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=";
 private Handler handler = new Handler(){
  public void handleMessagge (android.os.Message msg){
   switch(msg.what){
   case SUCCESS:    
    List<String> infos = (List<String>)msg.obj;
    tv1_info.setText("城市名称:"+infos.get(1)+"/n"+
            "当地时间"+infos.get(4)+"/n"+
      "城市温度:"+infos.get(5)+"/n"+
      "天气状况:"+infos.get(6)+"/n"+
      "穿衣指数:"+infos.get(11)+"/n");
    break;
   case ERROR:
    Toast.makeText(getApplicationContext(), "获取天气信息失败", 0).show();
    break;   
   }
  };
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.firstpage);
  setContentView(R.layout.activity_main);
  et_cityname = (EditText) findViewById(R.id.et_cityname);
  tv1_info = (TextView) findViewById(R.id.tv1_info);
  
  
 }
 /**
  * 查询按钮的点击事件
  * @param view
  */
 @SuppressWarnings("deprecation")
 public void click(View view){
  String cityname = et_cityname.getText().toString().trim();
  if(TextUtils.isEmpty(cityname)){
   Toast.makeText(this, "城市名不能为空", 0).show();
  }
  else{
   //北京
     final String path = basePath+URLEncoder.encode(cityname);
   new Thread(){
    private Object infos;

    public void run(){
     try{
      URL url=new URL(path);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      //以Get方式请求服务器数据
      conn.setRequestMethod("GET");
      conn.setConnectTimeout(5000);
      int code = conn.getResponseCode();
      if(code == 200){
       InputStream is = conn.getInputStream();
       //创建xml文件解析器
       XmlPullParser parser = Xml.newPullParser();
       //初始化xml解析器
       parser.setInput(is,"UTF-8");
       int type = parser.getEventType();
       //集合存放的所有的天气信息
       List<String> infos = new ArrayList<String>();
       while(type!=XmlPullParser.END_DOCUMENT){
        if(type==XmlPullParser.START_TAG){
         if("string".equals(parser.getName())){
          String str = parser.nextText();
          infos.add(str);
         }
        }
        type = parser.next();//只有没有解析到文档继续向下解析
       }
       Message msg = Message.obtain();
       msg.what = SUCCESS;
       msg.obj = infos;
       handler.sendMessage(msg);
      }
     }
     catch(Exception e){
      e.printStackTrace();
      Message msg = Message.obtain();
      msg.what = ERROR;
      msg.obj = infos;
      handler.sendMessage(msg);
     }
    }
   };
  }
 }
}


加载中
0
猴子请来的大爷
猴子请来的大爷

你的handler中的方法复写错了!

应该是 
public void handleMessage(Message msg) {}




返回顶部
顶部