- Tambahkan internet permission ke dalam manifest XML file.
- Tambahkan Retrofit dan Gson kedalam gradle build file
- Buat Object class yang akan di kirim atau di terima sebagai JSON file, jangan lupa bahwa semua fieldnya harus di declared dengan tipe String
- Buat ApiClient class
- Definisikan API Interface
- Gunakan ApiClient di Aplikasi kamu
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.appName"> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
... | |
</activity | |
</application> | |
</manifest> |
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
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | |
exclude group: 'com.android.support', module: 'support-annotations' | |
}) | |
compile 'com.android.support:appcompat-v7:26.+' | |
compile 'com.squareup.retrofit2:retrofit:2.3.0' | |
compile 'com.squareup.retrofit2:converter-gson:2.3.0' | |
testCompile 'junit:junit:4.12' | |
} |
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
public class User { | |
//dont forget to add @SerializedName annotation, the parameter is the Objectproperty name | |
@SerializedName("name") | |
private String name; | |
@SerializedName("email") | |
private String email; | |
//getters | |
public String getName() { | |
return name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
} |
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
public class ApiClient { | |
//this is the localhost url if you run the app using emulators | |
public static final String BASE_URL = "http://10.0.2.2/"; | |
public static Retrofit retrofit = null; | |
public static Retrofit getApiClient(){ | |
if(retrofit == null){ | |
retrofit = new Retrofit.Builder().baseUrl(BASE_URL). | |
addConverterFactory(GsonConverterFactory.create()).build(); | |
} | |
return retrofit; | |
} | |
} |
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
public interface ApiInterface { | |
// the parameter of get is the endpoint of the url. | |
// if your url is http://localhost:3000/users will looks like this | |
// Use a 'List' if you are fetching an array of Users | |
@GET("users") | |
Call<List<User>> getUsers(); | |
} |
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
private fetchData(){ | |
apiInterface = ApiClient.getApiClient().create(ApiInterface.class); | |
Call<List<User>> call = apiInterface.getSomething(); | |
call.enqueue(new Callback<List<User>>() { | |
@Override | |
public void onResponse(Call<List<User>> call, Response<List<User>> response) { | |
List<User> list = response.body(); | |
Log.d(TAG, "onResponse: " + list.get(0).getText()); | |
} | |
@Override | |
public void onFailure(Call<List<User>> call, Throwable t) { | |
Log.d(TAG, "onFailure: " + t.getMessage()); | |
} | |
}); | |
} |
Terkadang kamu harus membuat suatu class tambahan yang bisa menampung respon object dari server. Misal, kamu mengirim ("POST") user data melalui User object, akan tetapi jawaban (respond) dari server memiliki properties yang berbeda dari User class. Kamu bisa membuat
Class Result
yang bisa menampung respon dari server kemudian kamu bisa menambahkan Result Object kedalam Class User
. Tool yang saya pakai untuk membuat Result class ini adalah JsonSchemaToPOJO Caranya mudah, masukan respon (JSON) yang kamu dapatkan dari aplikasi POSTMAN kedalam formular yang ada di Json2Pojo.