android获取天气 android通过google api获取天气信息示例(android获取天气信息json)

网友投稿 476 2022-06-06


想了解android通过google api获取天气信息示例的相关内容吗,在本文为您仔细讲解android获取天气的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,获取天气信息,下面大家一起来学习吧。

android通过google API获取天气信息

复制代码 代码如下:

public class WeatherActivity extends Activity {
 private TextView txCity;
 private Button btnSearch;
 private Handler weatherhandler;
 private Dialog progressDialog;
 private Timer timer;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        timer = new Timer();
        txCity = (TextView)findViewById(R.id.txCity);
        btnSearch = (Button)findViewById(R.id.btnSearch);
        progressDialog = new AlertDialog.Builder(this)
        .setTitle("读取数据中")
        .setMessage("正在加载数据,请稍等")
        .create();

        weatherhandler = new Handler(){
         public void handleMessage(Message msg){
          final String cityName = txCity.getText().toString().trim();
          searchWeather(cityName);
          progressDialog.hide();
         }
        };

        btnSearch.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    progressDialog.show();
    timer.schedule(new TimerTask() {
     @Override
     public void run() {
      Message msg = new Message();
      msg.setTarget(weatherhandler);
      msg.sendToTarget();
     }
    },100);
   }
  });
    }
    private void searchWeather(String city){
     SAXParserFactory spf = SAXParserFactory.newInstance();
     try {
   SAXParser sp = spf.newSAXParser();
   XMLReader reader = sp.getXMLReader();
   XmlHandler handler = new XmlHandler();
   reader.setContentHandler(handler);
   URL url = new URL("http://google.com/ig/api?hl=zh-cn&weather="+URLEncoder.encode(city));
   InputStream is = url.openStream();
   InputStreamReader isr = new InputStreamReader(is, "GBK");
   InputSource source = new InputSource(isr);
   reader.parse(source);
   List<Weather>weatherList = handler.getWeatherList();
   TableLayout table = (TableLayout)findViewById(R.id.table);
   table.removeAllViews();
   for(Weather weather:weatherList){
    TableRow row = new TableRow(this);
    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    row.setGravity(Gravity.CENTER_VERTICAL);
    ImageView img = new ImageView(this);
    img.setImageDrawable(loadImage(weather.getImageUrl()));
    img.setMinimumHeight(80);
    row.addView(img);
    TextView day = new TextView(this);
    day.setText(weather.getDay());
    day.setGravity(Gravity.CENTER_HORIZONTAL);
    row.addView(day);
    TextView temp = new TextView(this);
    temp.setText(weather.getLowTemp()+"℃-"+weather.getHighTemp()+"℃");
    temp.setGravity(Gravity.CENTER_HORIZONTAL);
    row.addView(temp);
    TextView condition = new TextView(this);
    condition.setText(weather.getCondition());
    condition.setGravity(Gravity.CENTER_HORIZONTAL);
    row.addView(condition);
    table.addView(row);
   }
  } catch (Exception e) {
   e.printStackTrace();
   new AlertDialog.Builder(this)
    .setTitle("解析错误")
    .setMessage("获取天气数据失败,请稍候再试。")
    .setNegativeButton("确定", null)
    .show();  
  }

    }
 private Drawable loadImage(String imageUrl) {
  try {
   return Drawable.createFromStream((InputStream) new URL("http://google.com/"+imageUrl).getContent(), "test");
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 }
}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:win32 api消息窗口 win32 api实现简单的消息窗口示例(win32应用程序是什么意思)
下一篇:百度坐标转换 c#封装百度web服务geocoding api 、百度坐标转换示例(百度坐标转换工具)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~