Bu app modelimizde ki amacımız Retrofit Kütüphanesini öğrenmek..
İlk olarak uygulamamız nasıl duracak, tasarımımız kafanız da canlanması adına aşağıdaki görselleri inceleyebilirsiniz.
activity_main xml kodlarım;
- <?xml version=”1.0″ encoding=”utf-8″?>
<android.support.constraint.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”><TextView
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:textSize=”16sp”
android:layout_height=”wrap_content”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /><Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:text=”Get Current Weather Data”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/textView” /></android.support.constraint.ConstraintLayout>————————————————
*3 temel sınıfa ihtiyacım var:
- MainActivity.class //isterseniz başka bir sınıftandan da yönetebilirsiniz.
- RetrofitModel // Ben projem de WeatherResponse.java adında bir sınıf oluşturdum. Kodlarını aşağıya bırakacağım. RetrofitModel kısmında json’ımız @Serialized ettiğimiz kısım.
- Interface // Ben WeatherService.java adında bir sınıf oluşturdum. Bu sınıfta ise GET, POST, DELETE, PUT işlemlerini yapıyoruz.
Gelelim kodlarımıza..
MainActivity.class
package com.example.weatherapp; import android.graphics.Typeface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import com.ajts.androidmads.fontutils.FontUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class MainActivity extends AppCompatActivity { public static String BaseUrl = "http://api.openweathermap.org/"; public static String AppId = "59b72279fcf766f62815ebf956ba2774"; public static String lat = "41.0138"; public static String lon = "28.9497"; private TextView weatherData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); weatherData = findViewById(R.id.textView); Typeface typeface = Typeface.createFromAsset(getAssets(), "Lato-Bold.ttf"); FontUtils fontUtils = new FontUtils(); fontUtils.applyFontToView(weatherData, typeface); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getCurrentData(); } }); } void getCurrentData() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(BaseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); WeatherService service = retrofit.create(WeatherService.class); //WeatherService sınıfımdam bir tane obje yaratıyorum Call<WeatherResponse> call = service.getCurrentWeatherData(lat, lon, AppId); call.enqueue(new Callback<WeatherResponse>() { @Override public void onResponse(@NonNull Call<WeatherResponse> call, @NonNull Response<WeatherResponse> response) { if (response.code() == 200) { WeatherResponse weatherResponse = response.body(); assert weatherResponse != null; String stringBuilder = "Country: " + weatherResponse.sys.country + "\n" + "Temperature: " + weatherResponse.main.temp + "\n" + "Temperature(Min): " + weatherResponse.main.temp_min + "\n" + "Temperature(Max): " + weatherResponse.main.temp_max + "\n" + "Humidity: " + weatherResponse.main.humidity + "\n" + "Pressure: " + weatherResponse.main.pressure; weatherData.setText(stringBuilder); } } @Override public void onFailure(@NonNull Call<WeatherResponse> call, @NonNull Throwable t) { weatherData.setText(t.getMessage()); } }); } } WeatherResponse.java
package com.example.weatherapp; import com.google.gson.annotations.SerializedName; import java.util.ArrayList; public class WeatherResponse { @SerializedName("coord") public Coord coord; @SerializedName("sys") public Sys sys; @SerializedName("weather") public ArrayList<Weather> weather = new ArrayList<Weather>(); @SerializedName("main") public Main main; @SerializedName("wind") public Wind wind; @SerializedName("rain") public Rain rain; @SerializedName("clouds") public Clouds clouds; @SerializedName("dt") public float dt; @SerializedName("id") public int id; @SerializedName("name") public String name; @SerializedName("cod") public float cod; } class Weather { @SerializedName("id") public int id; @SerializedName("main") public String main; @SerializedName("description") public String description; @SerializedName("icon") public String icon; } class Clouds { @SerializedName("all") public float all; } class Rain { @SerializedName("3h") public float h3; } class Wind { @SerializedName("speed") public float speed; @SerializedName("deg") public float deg; } class Main { @SerializedName("temp") public float temp; @SerializedName("humidity") public float humidity; @SerializedName("pressure") public float pressure; @SerializedName("temp_min") public float temp_min; @SerializedName("temp_max") public float temp_max; } class Sys { @SerializedName("country") public String country; @SerializedName("sunrise") public long sunrise; @SerializedName("sunset") public long sunset; } class Coord { @SerializedName("lon") public float lon; @SerializedName("lat") public float lat; }
WeatherService.java import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; public interface WeatherService { @GET("data/2.5/weather?") Call<WeatherResponse> getCurrentWeatherData(@Query("lat") String lat, @Query("lon") String lon, @Query("APPID") String app_id); }
---------------------------------------- Github link: " github.com/yaseminuctas/WeatherApp "