android gets the weather information example through google api

  • 2020-06-01 10:58:26
  • OfStack

android gets weather information through 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(" Reading data ")
        .setMessage(" Please wait while the data is being loaded ")
        .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://www.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(" Parse error ")
    .setMessage(" Failed to obtain weather data. Please try again later. ")
    .setNegativeButton(" determine ", null)
    .show();  
  }

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


Related articles: