OpenWeatherMap JSON Problem bei Forecastabfrage

P

prof.dopenudel

Neues Mitglied
2
Hi,

möchte aktuell eine Wetterapp machen und habe dazu die OpenWeatherAPI im Einsatz.

Jetzt habe ich aber folgendes Problem:

Dem Support von OpenWeather habe ich bereits geschrieben. Die konnten mir nicht wirklich helfen.

Es gibt zwei Fragmente in dem Projekt. Eine mit

http://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&units=metric

und die Andere sollte eigentlich mit diesem Link funktionieren (auch laut Support):

http://api.openweathermap.org/data/2.5/forecast?lat=%s&lon=%s&units=metric

Mit dem Link werden aber gar keine Daten geladen. Habe ein Progressbar eingebaut, der aufhören sollte, falls irgendwas erhalten wird. Dreht sich ewig, im Gegensatz zum aktuellen Wetter. Progressbar lädt, sobald Daten da, er hört auf. Also die Daten werden auch richtig geladen.

Hier ist jetzt mal mein Code, um die Daten zu erhalten.
Code:
public class WeatherForecast {

private static final String OPEN_WEATHER_MAP_URL =
        "http://api.openweathermap.org/data/2.5/forecast?lat=5&lon=30&units=metric";

private static final String OPEN_WEATHER_MAP_API = "myAPI";

public interface AsyncResponse {
    void processFinish(String output1, String output2, String output3, String output4);
}

public static class placeIdTask extends AsyncTask<String, Void, JSONObject> {

    public AsyncResponse delegate = null;
    //Call back interface

    public placeIdTask(AsyncResponse asyncResponse) {
        delegate = asyncResponse;
        //Assigning call back interface through constructor
    }

    @Override
    protected JSONObject doInBackground(String... params) {

        JSONObject jsonWeather = null;
        try {
            jsonWeather = getWeatherJSON(params[0], params[1]);
        } catch (Exception e) {
            Log.d("Error", "Cannot process JSON results", e);
        }
        return jsonWeather;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        try {
            if(json != null){

                /*JSONObject jObject = new JSONObject("main");
                DateFormat df = DateFormat.getDateTimeInstance();

                JSONArray list = jObject.getJSONArray("list");

                    String city = json.getString("name").toUpperCase(Locale.US) + ", "
                            + json.getJSONObject("sys").getString("country");
                    String description = json.getJSONObject("list").getJSONObject("0")
                            .getJSONObject("weather").getJSONObject("0").getString("description");
                    String description1 = json.getString("description").toUpperCase(Locale.US);
                    String temperature = String.format("%.1f", jObject.getDouble("temp"))+ "°";
                    String updatedOn = df.format(new Date(json.getLong("dt")*1000));

                    delegate.processFinish(city, description, temperature, updatedOn);


                JSONObject details = json.getJSONArray("weatherforecast").getJSONObject(0);
                JSONObject main = json.getJSONObject("main");*/

           

                JSONObject details = json.getJSONArray("weather").getJSONObject(0);
                JSONObject main = json.getJSONObject("main");
                DateFormat df = DateFormat.getDateTimeInstance();

                String city = json.getString("name").toUpperCase(Locale.US) + ", " +
                        json.getJSONObject("sys").getString("country");
                String description = details.getString("description").toUpperCase(Locale.US);
                String temperature = String.format("%.1f", main.getDouble("temp"))+ "°";
                String updatedOn = df.format(new Date(json.getLong("dt")*1000));

                delegate.processFinish(city, description, temperature, updatedOn);

            }
        } catch (JSONException e) {

        }
    }
}

public static JSONObject getWeatherJSON(String lat, String lon){
    try {
        URL url = new URL(String.format(OPEN_WEATHER_MAP_URL, lat, lon));
        HttpURLConnection connection =
                (HttpURLConnection)url.openConnection();

        //connection.addRequestProperty("x-api-key", OPEN_WEATHER_MAP_API);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));

        StringBuffer json = new StringBuffer(1024);
        String tmp="";
        while((tmp=reader.readLine())!=null)
            json.append(tmp).append("\n");
        reader.close();

        JSONObject data = new JSONObject(json.toString());

        // This value will be 404 if the request was not successful
        if(data.getInt("cod") != 200){
            return null;
        }

        return data;
    }catch(Exception e){
        return null;
    }
}
}

Hier noch der Aufruf:

Code:
WeatherForecast.placeIdTask asyncTask = new WeatherForecast.placeIdTask(new WeatherForecast.AsyncResponse() {
            @Override
            public void processFinish(String weather_city, String weather_description,
                                      String weather_temperature, String weather_sunset, String weather_sunrise,
                                      String weather_updatedOn) {


                cityField1.setText(weather_city);
                detailsField1.setText(weather_description);
                sunRise1.setText(sunrise);
                sunSet1.setText(sunset);
                updatedField1.setText(update);
                currentTemperatureField1.setText(weather_temperature);

                linearLayout.setVisibility(View.VISIBLE);
                loading.setVisibility(View.GONE);
  }

So passiert leider nichts. Wenn man "forecast" wieder in "weather" ändert, geht es.
Die JSON Objekte sind unterschiedlich. Im Anhang ist die Struktur.

Oder gibt es eine einfachere Art, dass hier zu regeln?

Danke schon einmal
 

Anhänge

  • JSON Aktuell.JPG
    JSON Aktuell.JPG
    31,9 KB · Aufrufe: 303
  • JSON Forecast.JPG
    JSON Forecast.JPG
    31,1 KB · Aufrufe: 307
Kann mir keiner helfen? Komme einfach nicht drauf :1f605:
 

Ähnliche Themen

D
Antworten
17
Aufrufe
409
datNeMo
D
H
Antworten
2
Aufrufe
933
swa00
swa00
M
Antworten
3
Aufrufe
169
moin
M
Zurück
Oben Unten