Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created July 21, 2016 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvareztech/f53a0af0fbd2f22c0a09ba738bfc1773 to your computer and use it in GitHub Desktop.
Save alvareztech/f53a0af0fbd2f22c0a09ba738bfc1773 to your computer and use it in GitHub Desktop.
Aplicación Clima. Uso de la API OpenWeatherMap. Curso de desarrollo de aplicaciones.
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "tech.alvarez.clima"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}
package tech.alvarez.clima.models;
import java.util.ArrayList;
/**
* Created by Daniel Alvarez on 20/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class GroupRespuesta {
private ArrayList<WeatherRespuesta> list;
public ArrayList<WeatherRespuesta> getList() {
return list;
}
public void setList(ArrayList<WeatherRespuesta> list) {
this.list = list;
}
}
package tech.alvarez.clima.models;
/**
* Created by Daniel Alvarez on 20/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class Main {
private String temp;
private String humidity;
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
}
package tech.alvarez.clima;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import tech.alvarez.clima.models.GroupRespuesta;
import tech.alvarez.clima.models.WeatherRespuesta;
import tech.alvarez.clima.weather.OpenWeatherMapServicio;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.openweathermap.org/data/2.5/")
.addConverterFactory(GsonConverterFactory.create())
.build();
OpenWeatherMapServicio servicio = retrofit.create(OpenWeatherMapServicio.class);
Call<GroupRespuesta> groupRespuestaCall = servicio.obtenerGrupoClimas("3911925,3919968,3904906", "5ea80a3a5da74b767f1e1bd67efdadb8");
groupRespuestaCall.enqueue(new Callback<GroupRespuesta>() {
@Override
public void onResponse(Call<GroupRespuesta> call, Response<GroupRespuesta> response) {
GroupRespuesta respuesta = response.body();
for (int i = 0; i < respuesta.getList().size(); i++) {
WeatherRespuesta climaUnaCiudad = respuesta.getList().get(i);
Log.i("MIAPP", "Nombre: " + climaUnaCiudad.getName());
Log.i("MIAPP", "Humedad: " + climaUnaCiudad.getMain().getHumidity());
Log.i("MIAPP", "Temp: " + climaUnaCiudad.getMain().getTemp());
}
}
@Override
public void onFailure(Call<GroupRespuesta> call, Throwable t) {
}
});
/*
OpenWeatherMapServicio servicio = retrofit.create(OpenWeatherMapServicio.class);
Call<WeatherRespuesta> weatherRespuestaCall = servicio.obtenerClima("3911925", "5ea80a3a5da74b767f1e1bd67efdadb8");
// 3911925,3919968,3904906
weatherRespuestaCall.enqueue(new Callback<WeatherRespuesta>() {
@Override
public void onResponse(Call<WeatherRespuesta> call, Response<WeatherRespuesta> response) {
WeatherRespuesta respuesta = response.body();
Log.i("MIAPP", "Nombre: " + respuesta.getName());
Log.i("MIAPP", "Humedad: " + respuesta.getMain().getHumidity());
Log.i("MIAPP", "Temp: " + respuesta.getMain().getTemp());
}
@Override
public void onFailure(Call<WeatherRespuesta> call, Throwable t) {
}
});
*/
}
}
package tech.alvarez.clima.weather;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import tech.alvarez.clima.models.GroupRespuesta;
import tech.alvarez.clima.models.WeatherRespuesta;
/**
* Created by Daniel Alvarez on 20/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public interface OpenWeatherMapServicio {
@GET("weather?lang=es&units=metric")
Call<WeatherRespuesta> obtenerClima(@Query("id") String ide, @Query("appid") String apiKey);
@GET("group?lang=es&units=metric")
Call<GroupRespuesta> obtenerGrupoClimas(@Query("id") String ide, @Query("appid") String apiKey);
}
package tech.alvarez.clima.models;
/**
* Created by Daniel Alvarez on 20/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class WeatherRespuesta {
private String name;
private Main main;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment