-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd51bfa
commit 2891462
Showing
33 changed files
with
826 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module external.system.id="GRADLE" type="JAVA_MODULE" version="4"> | ||
<component name="FacetManager"> | ||
<facet type="android-gradle" name="Android-Gradle"> | ||
<configuration> | ||
<option name="GRADLE_PROJECT_PATH" value=":" /> | ||
</configuration> | ||
</facet> | ||
<facet type="android" name="Android"> | ||
<configuration> | ||
<option name="ALLOW_USER_CONFIGURATION" value="false" /> | ||
</configuration> | ||
</facet> | ||
</component> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 23 | ||
buildToolsVersion "24.0.0 rc3" | ||
|
||
defaultConfig { | ||
applicationId "com.android.gifts.retrofit" | ||
minSdkVersion 14 | ||
targetSdkVersion 23 | ||
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:23.3.0' | ||
compile 'com.squareup.retrofit2:retrofit:2.0.2' | ||
compile 'com.squareup.retrofit2:converter-gson:2.0.2' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in /Users/ahmedadel/Downloads/adt-bundle-mac-x86_64-20140702/sdk/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
13 changes: 13 additions & 0 deletions
13
app/src/androidTest/java/com/android/gifts/retrofit/ApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.android.gifts.retrofit; | ||
|
||
import android.app.Application; | ||
import android.test.ApplicationTestCase; | ||
|
||
/** | ||
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> | ||
*/ | ||
public class ApplicationTest extends ApplicationTestCase<Application> { | ||
public ApplicationTest() { | ||
super(Application.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.android.gifts.retrofit"> | ||
|
||
<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=".RetrofitMainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/android/gifts/retrofit/GetWeatherApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.android.gifts.retrofit; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.http.GET; | ||
import retrofit2.http.Query; | ||
|
||
/** | ||
* Created by ahmedadel on 26/04/16. | ||
*/ | ||
public interface GetWeatherApi { | ||
|
||
/** | ||
* Synchronous and Asynchronous background requests through Retrofit | ||
* In regard to service interface declaration in Retrofit 1.9, you have to to declare a synchronous function, | ||
* and an asynchronous separately, But on Retrofit 2.0, | ||
* it is far more simple since you can declare with only just a single pattern. | ||
* @param cityName London as an example | ||
* @param units metrics as an example | ||
* @param appId that the developer got from OpenWeatherMap after singing up | ||
* @return | ||
*/ | ||
@GET("/data/2.5/weather") | ||
Call<WeatherDataBean> getWeatherFromApiSyncAndAsync( | ||
@Query("q") String cityName, | ||
@Query("units") String units, | ||
@Query("APPID") String appId); | ||
|
||
} |
212 changes: 212 additions & 0 deletions
212
app/src/main/java/com/android/gifts/retrofit/RetrofitMainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package com.android.gifts.retrofit; | ||
|
||
import android.os.AsyncTask; | ||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.LinearLayout; | ||
import android.widget.ProgressBar; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import java.io.IOException; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
public class RetrofitMainActivity extends AppCompatActivity implements View.OnClickListener { | ||
|
||
/** | ||
* Define the root OpenWeatherMap URL and your OpenWeatherMap AppID | ||
*/ | ||
private static final String API_URL = "http://api.openweathermap.org"; | ||
private static final String CITY_NAME = "London"; | ||
private static final String UNITS = "metric"; | ||
private static final String APP_ID = "c6afdab60aa89481e297e0a4f19af055"; | ||
|
||
/** | ||
* Instance variables to represent the "London Current Weather Synchronously" | ||
* and "London Current Weather Asynchronously" buttons, | ||
* "Temperature", "Pressure" and "Humidity" TextViews and loadingProgressbar | ||
*/ | ||
private Button getLondonCurrentWeatherSync, getLondonCurrentWeatherAsync; | ||
private TextView temperatureTextView, pressureTextView, humidityTextView; | ||
private ProgressBar loadingProgressBar; | ||
private LinearLayout getLondonCurrentWeatherLinearLayout; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_retrofit_main); | ||
|
||
/** | ||
* Instantiate the variables we declared above using the ID values | ||
* we specified in the layout XML file. | ||
*/ | ||
getLondonCurrentWeatherSync = (Button) findViewById(R.id.activity_retrofit_main_get_weather_sync_btn); | ||
getLondonCurrentWeatherAsync = (Button) findViewById(R.id.activity_retrofit_main_get_weather_async_btn); | ||
getLondonCurrentWeatherLinearLayout = (LinearLayout) findViewById(R.id.activity_retrofit_main_root_ll); | ||
temperatureTextView = (TextView) findViewById(R.id.activity_retrofit_main_temperature_tv); | ||
pressureTextView = (TextView) findViewById(R.id.activity_retrofit_main_pressure_tv); | ||
humidityTextView = (TextView) findViewById(R.id.activity_retrofit_main_humidity_tv); | ||
loadingProgressBar = (ProgressBar) findViewById(R.id.activity_retrofit_main_pb); | ||
|
||
/** | ||
* Add a listener to getLondonCurrentWeatherSync and getLondonCurrentWeatherAsync so that we can handle presses | ||
*/ | ||
getLondonCurrentWeatherSync.setOnClickListener(this); | ||
getLondonCurrentWeatherAsync.setOnClickListener(this); | ||
|
||
} | ||
|
||
@Override | ||
public void onClick(View view) { | ||
switch (view.getId()) { | ||
/** | ||
* make a synchronous background request | ||
*/ | ||
case R.id.activity_retrofit_main_get_weather_sync_btn: | ||
getWeatherSync(); | ||
break; | ||
/** | ||
* make an Asynchronous background request | ||
*/ | ||
case R.id.activity_retrofit_main_get_weather_async_btn: | ||
getWeatherAsync(); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* getWeatherAsync() method will make an asynchronous background request | ||
* by using Call Interface built in Retrofit Library | ||
*/ | ||
private void getWeatherAsync() { | ||
getLondonCurrentWeatherLinearLayout.setVisibility(View.INVISIBLE); | ||
loadingProgressBar.setVisibility(View.VISIBLE); | ||
/** | ||
* To make REST API call through Android Retrofit Library we may first need to build an instance of Retrofit class. | ||
* This class is the main class of Retrofit Library which executes all the requests. | ||
*/ | ||
Retrofit retrofit = new Retrofit.Builder() | ||
.baseUrl(API_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build(); | ||
/** | ||
* Next you need to create an instance of your Interface GetWeatherApi. | ||
* since create() method that is inside retrofit object return the parsed response in | ||
* GetWeatherApi Interface format | ||
*/ | ||
GetWeatherApi getWeatherApi = retrofit.create(GetWeatherApi.class); | ||
/** | ||
* After this, call getWeatherFromApiSyncAndAsync() method from getWeatherApi object that return an instance of | ||
* Call Retrofit Interface then call enqueue() method to make an asynchronous API request, and implement inside it | ||
* the CallBack Interface Listener "Observer" since this Callback Interface has to methods onResponse() | ||
* that is fire once a successive response is returned from OpenWeatherMap API and onFailure() | ||
* that is fire once an error occurs | ||
*/ | ||
Call<WeatherDataBean> call = getWeatherApi.getWeatherFromApiSyncAndAsync(CITY_NAME, UNITS, APP_ID); | ||
call.enqueue(new Callback<WeatherDataBean>() { | ||
@Override | ||
public void onResponse(Call<WeatherDataBean> call, Response<WeatherDataBean> response) { | ||
updateUI(response.body()); | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<WeatherDataBean> call, Throwable t) { | ||
loadingProgressBar.setVisibility(View.GONE); | ||
Toast.makeText(RetrofitMainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* updateUI() method will be used once a successive response is returned from OpenWeatherMap API | ||
* | ||
* @param weatherDataBean | ||
*/ | ||
private void updateUI(WeatherDataBean weatherDataBean) { | ||
loadingProgressBar.setVisibility(View.GONE); | ||
if (weatherDataBean != null) { | ||
getLondonCurrentWeatherLinearLayout.setVisibility(View.VISIBLE); | ||
temperatureTextView.setText("Temperature : " + weatherDataBean.getMain().getTemp() + " Celsius"); | ||
pressureTextView.setText("Pressure : " + weatherDataBean.getMain().getPressure()); | ||
humidityTextView.setText("Humidity : " + weatherDataBean.getMain().getHumidity()); | ||
} | ||
} | ||
|
||
/** | ||
* getWeatherSync() method will make an synchronous background request | ||
* by using Call Interface built in Retrofit Library | ||
*/ | ||
private void getWeatherSync() { | ||
GetWeatherSync getWeatherSync = new GetWeatherSync(); | ||
getWeatherSync.execute(); | ||
} | ||
|
||
private class GetWeatherSync extends AsyncTask<Void, Void, WeatherDataBean> { | ||
|
||
Retrofit retrofit; | ||
|
||
@Override | ||
protected void onPreExecute() { | ||
getLondonCurrentWeatherLinearLayout.setVisibility(View.INVISIBLE); | ||
loadingProgressBar.setVisibility(View.VISIBLE); | ||
/** | ||
* To make REST API call through Android Retrofit Library we may first need to build an instance of Retrofit class. | ||
* This class is the main class of Retrofit Library which executes all the requests. | ||
*/ | ||
retrofit = new Retrofit.Builder() | ||
.baseUrl(API_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
protected WeatherDataBean doInBackground(Void... params) { | ||
WeatherDataBean weatherDataBean = null; | ||
try { | ||
/** | ||
* Next you need to create an instance of your Interface GetWeatherApi. | ||
* since create() method that is inside retrofit object return the parsed response in | ||
* GetWeatherApi Interface format | ||
*/ | ||
GetWeatherApi getWeatherApi = retrofit.create(GetWeatherApi.class); | ||
/** | ||
* After this, call getWeatherFromApiSyncAndAsync() method from getWeatherApi object that return an instance of | ||
* Call Interface and then call execute() method since this method make a request synchronous | ||
* so it needs to be handled in background thread like AsyncTask Class to avoid | ||
* NetworkOnMainThreadException runtime error. | ||
*/ | ||
Call<WeatherDataBean> call = getWeatherApi.getWeatherFromApiSyncAndAsync(CITY_NAME, UNITS, APP_ID); | ||
Response<WeatherDataBean> response = call.execute(); | ||
weatherDataBean = response.body(); | ||
} catch (final IOException e) { | ||
runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
loadingProgressBar.setVisibility(View.GONE); | ||
Toast.makeText(RetrofitMainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
} | ||
|
||
return weatherDataBean; | ||
} | ||
|
||
/** | ||
* Get the returned object from doInBackground() method and get from this object the | ||
* necessary data that we want. | ||
* | ||
* @param weatherDataBean is the returned object that came from doInBackground() merhod | ||
*/ | ||
@Override | ||
protected void onPostExecute(WeatherDataBean weatherDataBean) { | ||
updateUI(weatherDataBean); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/android/gifts/retrofit/WeatherDataBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.android.gifts.retrofit; | ||
|
||
/** | ||
* Created by ahmedadel on 26/04/16. | ||
*/ | ||
public class WeatherDataBean { | ||
|
||
private Main main; | ||
|
||
public Main getMain() { | ||
return main; | ||
} | ||
|
||
public void setMain(Main main) { | ||
this.main = main; | ||
} | ||
|
||
class Main { | ||
double temp; | ||
double pressure; | ||
int humidity; | ||
|
||
public double getTemp() { | ||
return temp; | ||
} | ||
|
||
public void setTemp(double temp) { | ||
this.temp = temp; | ||
} | ||
|
||
public double getPressure() { | ||
return pressure; | ||
} | ||
|
||
public void setPressure(double pressure) { | ||
this.pressure = pressure; | ||
} | ||
|
||
public int getHumidity() { | ||
return humidity; | ||
} | ||
|
||
public void setHumidity(int humidity) { | ||
this.humidity = humidity; | ||
} | ||
} | ||
} |
Oops, something went wrong.