2016-03-27 1 views
0

Ich bin neu in Retrofit. Ich versuche, OpenWeatherMap mit Retrofit abzufragen und einige Wetterdaten zurückzugeben.Android - mit Retrofit, um Daten zu bekommen, aber nicht in Objekt

Hier einige meiner Code:

public static void getCurForecast(final Resources resources, final String zipCode, final String countryCode, final ForecastListener listener) { 
     new AsyncTask<Void, Void, CurForecastResponse>() { 

      @Override 
      protected CurForecastResponse doInBackground(Void... params) { 
       Log.d("TAG","in CurForecastResponse"); 
       RestAdapter restAdapter = new RestAdapter.Builder() 
         .setEndpoint("http://api.openweathermap.org") 
         .build(); 
       //String zipCode = "78613"; 
       //String countryCode = "us"; 
       ForecastRequest service = restAdapter.create(ForecastRequest.class); 
       try { 
        String zipCodeFormat = zipCode + ",us"; 
        Log.d("TAG","zipCodeFormat: " + zipCodeFormat); 
        CurForecastResponse temp = service.getCurForecast(zipCodeFormat, resources.getString(R.string.open_weather_api_key)); 
        if(temp == null){ 
         Log.d("TAG","TEMP = NULL!"); 
        } 
        else { 
         Log.d("TAG","Weather: " + temp.name); 
        } 
        return temp;//service.getCurForecast(zipCodeFormat, resources.getString(R.string.open_weather_api_key)); 
       } catch (RetrofitError error) { 
        Log.w("ForecastModule", "Forecast error: " + error.getMessage()); 
        return null; 
       } 
      }; 

Hier ist ein Teil des JSON-Objekt ist:

public class CurForecastResponse { 

    public int cod; 
    public String base; 
    public String name; 
    public ForecastCurrently currently; 
    public ForecastHourly hourly; 
    public Weather main; 

    public CurForecastResponse() { 
     Log.d("TAG","CurForecastResponse Constructor"); 
    } 
    public class weather { 
     int id; 
     String main; 
     String description; 
     String icon; 
     public weather(int id, String main, String description, String icon) { 
     this.id = id; 
     this.main = main; 
     this.description = description; 
     this.icon = icon; 
    } 
     public int getId() {return id; } 
     public String getDescription() {return description; } 
     public String getIcon() {return icon; } 
     public String getMain() {return main; } 
    } 

Hier ist die ForecastRequest Klasse

public interface ForecastRequest { 

    //Open Weather Forecast API 
    @GET("/data/2.5/weather?") 
    CurForecastResponse getCurForecast(@Query("zip") String zipCode, 
             @Query("apiKey") String apiKey); 
} 

Hier ist ein Beispiel von JSON-Antwort

{"coord":{"lon":145.77,"lat":-16.92}, 
"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}], 
"base":"cmc stations", 
"main":{"temp":305.15,"pressure":1013,"humidity":52,"temp_min":305.15,"temp_max":305.15},"wind":{"speed":5.7,"deg":40},"clouds":{"all":20},"dt":1459137600,"sys":{"type":1,"id":8166,"message":0.0092,"country":"AU","sunrise":1459110134,"sunset":1459153277},"id":2172797,"name":"Cairns","cod":200} 

Ich habe Probleme, Daten in die Wetterklasse zu bekommen.

+0

Bitte geben Sie die Version verwenden können Nachrüstung, die Sie verwenden. –

+0

die Version, die ich benutze, ist v1.9 –

+0

muss ein Array für meine Daten erstellt werden .. duh ... –

Antwort

1

In Retrofit2 sollten Sie Ihren Rückgabewert mit Anruf umhüllen.

Call-Instanzen können entweder synchron oder asynchron ausgeführt werden. Jede Instanz kann nur einmal verwendet werden, aber durch den Aufruf von clone() wird eine neue Instanz erstellt, die verwendet werden kann.

So schnell Probe von seinem Github-Repository ist:

public final class SimpleService { 

public static final String API_URL = "https://api.github.com"; 

    public static class Contributor { 
    public final String login; 
    public final int contributions; 

    public Contributor(String login, int contributions) { 
     this.login = login; 
     this.contributions = contributions; 
    } 
    } 

    public interface GitHub { 
    @GET("/repos/{owner}/{repo}/contributors") 
    Call<List<Contributor>> contributors(
     @Path("owner") String owner, 
     @Path("repo") String repo); 
    } 

    public static void main(String... args) throws IOException { 
    // Create a very simple REST adapter which points the GitHub API. 
    Retrofit retrofit = new Retrofit.Builder() 
     .baseUrl(API_URL) 
     .addConverterFactory(GsonConverterFactory.create()) 
     .build(); 

    // Create an instance of our GitHub API interface. 
    GitHub github = retrofit.create(GitHub.class); 

    // Create a call instance for looking up Retrofit contributors. 
    Call<List<Contributor>> call = github.contributors("square", "retrofit"); 

    // Fetch and print a list of the contributors to the library. 
    List<Contributor> contributors = call.execute().body(); 
    for (Contributor contributor : contributors) { 
     System.out.println(contributor.login + " (" + contributor.contributions + ")"); 
    } 
    } 
} 

Beachten Sie die Zeile:

List<Contributor> contributors = call.execute().body(); 

läuft synchronously. Verwenden Sie für asynchronous Anruf call.enqueue und überschreiben Sie die Rückrufmethoden.

1

Gemäß der json Antwort

{ 
    "coord": { 
     "lon": 145.77, 
     "lat": -16.92 
    }, 
    "weather": [{ 
     "id": 801, 
     "main": "Clouds", 
     "description": "few clouds", 
     "icon": "02d" 
    }], 
    "base": "cmc stations", 
    "main": { 
     "temp": 305.15, 
     "pressure": 1013, 
     "humidity": 52, 
     "temp_min": 305.15, 
     "temp_max": 305.15 
    }, 
    "wind": { 
     "speed": 5.7, 
     "deg": 40 
    }, 
    "clouds": { 
     "all": 20 
    }, 
    "dt": 1459137600, 
    "sys": { 
     "type": 1, 
     "id": 8166, 
     "message": 0.0092, 
     "country": "AU", 
     "sunrise": 1459110134, 
     "sunset": 1459153277 
    }, 
    "id": 2172797, 
    "name": "Cairns", 
    "cod": 200 
    } 

Ihre pojo Klasse (Modell Klasse) so sein sollte

public class CurForecastResponse { 

    public Coord coord; 
    public List<Weather> weather; 
    public String base; 
    public Main main; 
    public Wind wind; 
    public Clouds clouds; 
    public double dt; 
    public Sys sys; 
    public int id; 
    public String name; 
    public int cod; 

    public CurForecastResponse() { 
     Log.d("TAG", "CurForecastResponse Constructor"); 
    } 

    public class Coord { 
     public double lon; 
     public double lat; 
    } 

    public class Weather { 
     public int id; 
     public String main; 
     public String description; 
     public String icon; 
    } 

    public class Main { 
     public double temp; 
     public double pressure; 
     public double humidity; 
     public double temp_min; 
     public double temp_max; 
    } 

    public class Wind{ 
     public double speed; 
     public double deg; 
    } 

    public class Clouds{ 
     public double all; 
    } 

    public class Sys{ 
     public int type; 
     public int id; 
     public double message; 
     public String country; 
     public int sunrise; 
     public int sunset; 
    } 
} 

Sie Setter und Getter in diesem

Verwandte Themen