Skip to content

Commit 9b3e8bc

Browse files
author
Santiago Faci
committed
New sample: consume API using RetroFit
1 parent 4904313 commit 9b3e8bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+906
-0
lines changed

themealdb/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

themealdb/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

themealdb/app/build.gradle

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
namespace 'space.harbour.themealdb'
7+
compileSdk 32
8+
9+
defaultConfig {
10+
applicationId "space.harbour.themealdb"
11+
minSdk 21
12+
targetSdk 32
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
33+
// Lombok: Download the plugin zip file, extract it and copy to $android_studio_dir/plugins
34+
compileOnly "org.projectlombok:lombok:1.18.24"
35+
annotationProcessor 'org.projectlombok:lombok:1.18.24'
36+
implementation "androidx.room:room-runtime:2.4.3"
37+
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
38+
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
39+
implementation 'com.squareup.picasso:picasso:2.8'
40+
annotationProcessor "androidx.room:room-compiler:2.4.3"
41+
implementation 'androidx.appcompat:appcompat:1.5.1'
42+
implementation 'com.google.android.material:material:1.7.0'
43+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
44+
}

themealdb/app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:dataExtractionRules="@xml/data_extraction_rules"
11+
android:fullBackupContent="@xml/backup_rules"
12+
android:icon="@mipmap/ic_launcher"
13+
android:label="@string/app_name"
14+
android:roundIcon="@mipmap/ic_launcher_round"
15+
android:supportsRtl="true"
16+
android:theme="@style/Theme.Themealdb"
17+
tools:targetApi="31">
18+
<activity
19+
android:name=".MainActivity"
20+
android:exported="true">
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN" />
23+
24+
<category android:name="android.intent.category.LAUNCHER" />
25+
</intent-filter>
26+
27+
<meta-data
28+
android:name="android.app.lib_name"
29+
android:value="" />
30+
</activity>
31+
</application>
32+
33+
</manifest>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package space.harbour.themealdb;
2+
3+
import android.os.Bundle;
4+
import android.widget.ArrayAdapter;
5+
import android.widget.ListView;
6+
import android.widget.Toast;
7+
8+
import androidx.appcompat.app.AppCompatActivity;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import retrofit2.Call;
14+
import retrofit2.Callback;
15+
import retrofit2.Response;
16+
import space.harbour.themealdb.api.TheMealDbApi;
17+
import space.harbour.themealdb.api.TheMealDbApiInterface;
18+
import space.harbour.themealdb.domain.Category;
19+
import space.harbour.themealdb.domain.CategoryApiList;
20+
21+
public class MainActivity extends AppCompatActivity {
22+
23+
ArrayAdapter<Category> categoryAdapter;
24+
List<Category> categoryList;
25+
26+
@Override
27+
protected void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
setContentView(R.layout.activity_main);
30+
31+
categoryList = new ArrayList<>();
32+
categoryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, categoryList);
33+
ListView listView = findViewById(R.id.categoryList);
34+
listView.setAdapter(categoryAdapter);
35+
36+
loadMeals();
37+
}
38+
39+
private void loadMeals() {
40+
TheMealDbApiInterface api = TheMealDbApi.buildInstance();
41+
Call<CategoryApiList> callProducts = api.getAllCategories();
42+
callProducts.enqueue(new Callback<CategoryApiList>() {
43+
@Override
44+
public void onResponse(Call<CategoryApiList> call, Response<CategoryApiList> response) {
45+
CategoryApiList categoryApiList = response.body();
46+
categoryList.addAll(categoryApiList.getCategories());
47+
categoryAdapter.notifyDataSetChanged();
48+
}
49+
50+
@Override
51+
public void onFailure(Call<CategoryApiList> call, Throwable throwable) {
52+
Toast.makeText(getApplicationContext(), "An error has occurred", Toast.LENGTH_LONG).show();
53+
categoryList.clear();
54+
categoryAdapter.notifyDataSetChanged();
55+
Toast.makeText(getApplicationContext(), throwable.getMessage(), Toast.LENGTH_LONG).show();
56+
}
57+
});
58+
}
59+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package space.harbour.themealdb.api;
2+
3+
public class Constants {
4+
5+
public final static String BASE_URL = "https://www.themealdb.com/api/json/v1/1/";
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package space.harbour.themealdb.api;
2+
3+
import static space.harbour.themealdb.api.Constants.BASE_URL;
4+
5+
import retrofit2.Retrofit;
6+
import retrofit2.converter.gson.GsonConverterFactory;
7+
8+
public class TheMealDbApi {
9+
10+
public static TheMealDbApiInterface buildInstance() {
11+
Retrofit retrofit = new Retrofit.Builder()
12+
.baseUrl(BASE_URL)
13+
.addConverterFactory(GsonConverterFactory.create())
14+
.build();
15+
return retrofit.create(TheMealDbApiInterface.class);
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package space.harbour.themealdb.api;
2+
3+
import retrofit2.Call;
4+
import retrofit2.http.GET;
5+
import retrofit2.http.Query;
6+
import space.harbour.themealdb.domain.CategoryApiList;
7+
import space.harbour.themealdb.domain.MealApiList;
8+
9+
public interface TheMealDbApiInterface {
10+
11+
@GET("search.php")
12+
Call<MealApiList> getMealsByName(@Query("s") String name);
13+
14+
@GET("categories.php")
15+
Call<CategoryApiList> getAllCategories();
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package space.harbour.themealdb.domain;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class Category {
11+
12+
private int idCategory;
13+
private String strCategory;
14+
private String strCategoryDescription;
15+
16+
@Override
17+
public String toString() {
18+
return strCategory;
19+
}
20+
}

0 commit comments

Comments
 (0)