Soru & Cevap

android hava durumu uygulama

27.07.2016 - 01:43

android hava durumu uygulaması yapmaya çalışıyorum kodların bu kısmı sıcaklık değerini vermesi gerekiyor.ama vermiyor direk catch içindeki toast mesajını veriyor.google_weather olarak tanımladığım kısımda değişiklik yaptım yien sonuç  alamadım.daha önceden https://www.google.com/ig/api?weather=  kullandım yine sonuç alamadım

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editText;
    private Button button;
    private TextView textView;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;

    private  final static String GOOGLE="http://www.google.com";
    private  final static String GOOGLE_WEATHER="http://api.openweathermap.org/data/2.5/weather?q=%s&units=metric=";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
    }

    private  void  initialize(){
        button=(Button)findViewById(R.id.button);
        editText=(EditText)findViewById(R.id.editText);
        textView=(TextView)findViewById(R.id.textView);
        textView2=(TextView)findViewById(R.id.textView2);
        textView3=(TextView)findViewById(R.id.textView3);
        textView4=(TextView)findViewById(R.id.textView4);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                String location=editText.getText().toString();

                loadData(location);
                break;
        }
    }
    private  void loadData(String location){
        try{
            URL url= new URL(GOOGLE_WEATHER + location);
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp=spf.newSAXParser();
            XMLReader reader=sp.getXMLReader();
            weather handler=new weather();
            reader.setContentHandler(handler);
            reader.parse(new InputSource(url.openStream()));

            textView4.setText(handler.getDate());
            textView.setText(handler.getTempC());
            textView2.setText(handler.getHumidity());
            textView3.setText(handler.getCondition());
        }
        catch (Exception e){
            Toast.makeText(getBaseContext(),"ememem", Toast.LENGTH_LONG).show();
            editText.requestFocus();
        }
    }
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mervee.havadurumuu" >
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.jar.Attributes;


public class weather extends DefaultHandler {
    private  String date;
    private  String tempF;
    private  String tempC;
    private  String humidity;
    private  String condition;
    private  String icon;


    private ArrayList<String> daylist=new ArrayList<String>();
    private ArrayList<String> iconlist=new ArrayList<String>();
    private ArrayList<String> conditionlist=new ArrayList<String>();

    private int order=0;

    //@Override
    public  void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {

        String data = "";
        if (localName.equals("forecast_date")) {
            setDate(attributes.getValue("data"));

        } else if (localName.equals("temp_f")) {
            setTempF(attributes.getValue("data") + "*F");
        } else if (localName.equals("temp_c")) {
            setTempC(attributes.getValue("data") + "*C");
        } else if (localName.equals("forecast_conditions")) {
            order = 1;
        } else if (localName.equals("icon")) {
            data = attributes.getValue("data");
            if (order == 0) {
                setIcon(data);
            } else {
                iconlist.add(data);

            }
        } else if (localName.equals("day_of_week")) {
            if (order != 0) {
                daylist.add(getWeekDay(attributes.getValue("data")));
            }
        } else if (localName.equals("condition")) {
            data = attributes.getValue("data");
            if (order == 0) {
                setCondition(data);
            } else {
                conditionlist.add(data);
            }
        }
    }



    private String getWeekDay(String day) {
        if(day.equals("Mon")){
            return "Monday";
        }else if(day.equals("Tue")){
            return  "Tuesday";
        }else if(day.equals("Wed")){
            return  "Wednesday";
        }else if(day.equals("Thu")){
            return  "Thursday";
        }else if(day.equals("Fri")){
            return  "Friday";
        }else if(day.equals("sat")){
            return  "Saturday";
        }else{
            return "Sunday";
        }
    }
    public String getDate(){
        return  date;

    }
    private void setDate(String date) {
        this.date=date;
    }
    public  String getTempF(){
        return tempF;
    }
    private void setTempF(String tempF) {
       this.tempF=tempF;
    }
    public  String getTempC(){
        return  tempC;
    }
    private void setTempC(String tempC) {
        this.tempC=tempC;
    }
    public  String getHumidity(){
        return  humidity;
    }
    public void setHumidity(String humidity){
        this.humidity=humidity;
    }
    public  String getCondition(){
        return  condition;
    }
    public void setCondition(String condition){
        this.condition=condition;
    }
    public String getIcon(){
        return  icon;
    }
    public void setIcon(String icon) {
        this.icon=icon;
    }

}

 

108 Görüntülenme

0 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.