Android Open Source - havadurumu Hava Durumu






From Project

Back to project page havadurumu.

License

The source code is released under:

Apache License

If you think the Android project havadurumu listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tr.kodgraf.havadurumusamp;
/*w w  w.  jav a2  s  .  co  m*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.crypto.spec.IvParameterSpec;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpConnection;
import org.apache.http.HttpConnectionMetrics;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class HavaDurumu extends Activity {
  
  String baseWeatherURL = "http://weather.yahooapis.com/forecastrss?u=c&w=";
  String fullWeatherURL = null;
  String baseGeoPlanetURL = "http://where.yahooapis.com/v1/places.q('";
  String appid = "')?appid=dj0yJmk9MHJsMFl0NWdGbFVYJmQ9WVdrOU1XNUtNbmRUTnpZbWNHbzlNVE00TlRBek9USTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD1lYQ";
  TextView tvResult, tvWoeidResult;
  ImageView ivWeatherImage;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tvResult = (TextView) findViewById(R.id.tvResult);
        tvWoeidResult = (TextView) findViewById(R.id.tvWoeidResult);
        
        final EditText etCity = (EditText) findViewById(R.id.etCity);
        
        ivWeatherImage = (ImageView) findViewById(R.id.ivWeatherImage);
        
        Button btnWoeidSearch = (Button) findViewById(R.id.btnWoeidSearch);
        btnWoeidSearch.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        String fullPlanetURL = baseGeoPlanetURL + etCity.getText().toString() + appid;
        new GeoPlanetXMLDataReader().execute(fullPlanetURL);
      }
    });
        
        Button btnGetWeather = (Button) findViewById(R.id.btnGetWeather);
        btnGetWeather.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        new WeatherXMLDataReader().execute(fullWeatherURL);
      }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private class WeatherXMLDataReader extends AsyncTask<String, Integer, String> {
        
        @Override
        protected String doInBackground(String... urls) {
          
          try {
            URL myAPIURL = new URL(urls[0]); 
            
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xml = sp.getXMLReader();
            
            XmlDataHandler xmlHandler = new XmlDataHandler();
            xml.setContentHandler(xmlHandler);
            
            xml.parse(new InputSource(myAPIURL.openStream()));
            
            new DownloadImage().execute(xmlHandler.imageURL);
            return xmlHandler.getResult();
            
          } catch (Exception e) {
            return e.toString();
          }
        }
  
        @Override
        protected void onPostExecute(String result) {
          tvResult.setText(result);
        }
    }
    
    private class GeoPlanetXMLDataReader extends AsyncTask<String, Integer, String> {
    
      @Override
      protected String doInBackground(String... urls) {
        
        try {
          URL myAPIURL = new URL(urls[0]); 
          
          SAXParserFactory spf = SAXParserFactory.newInstance();
          SAXParser sp = spf.newSAXParser();
          XMLReader xml = sp.getXMLReader();
          
          GeoPlanetDataHandler geoHandler = new GeoPlanetDataHandler();
          xml.setContentHandler(geoHandler);
          
          xml.parse(new InputSource(myAPIURL.openStream()));
          
          return geoHandler.woeidNumber;
          
        } catch (Exception e) {
          return e.toString();
        }
      }

      @Override
      protected void onPostExecute(String result) {
        if( result != null) {
          tvWoeidResult.setText("WOEID: " + result);
          fullWeatherURL = baseWeatherURL + result;
        } else {
          tvWoeidResult.setText("WOEID BULUNAMADI");
        }
      }
}
    
    private class DownloadImage extends AsyncTask<String, Integer, Drawable> {

        @Override
        protected Drawable doInBackground(String... urls) {
            // This is done in a background thread
          try {
                URL url = new URL(urls[0]);
                InputStream in = url.openStream();

                // Read the inputstream 
                BufferedInputStream buf = new BufferedInputStream(in);

                // Convert the BufferedInputStream to a Bitmap
                Bitmap bMap = BitmapFactory.decodeStream(buf);
                
                if (in != null) {
                    in.close();
                }
                
                if (buf != null) {
                    buf.close();
                }

                return new BitmapDrawable(bMap);

            } catch (Exception e) {
                Log.e("Error reading file", e.toString());
            }

            return null;
        }

        protected void onPostExecute(Drawable image)
        {
            ivWeatherImage.setImageDrawable(image);
        }
    }
}




Java Source Code List

com.tr.kodgraf.havadurumusamp.GeoPlanetDataHandler.java
com.tr.kodgraf.havadurumusamp.HavaDurumu.java
com.tr.kodgraf.havadurumusamp.XmlDataHandler.java