Example :
My profile's ID is 1. So the url become www.url.com/endpoint?paremeter=1In this example i just use one data profile with ID = 1. You can improve your project by creating much profile.
As you can see at the video above, after success get data profile, the response will display all attribute of my profile like ID, NAME, JOB and AGE
PREPARATION
First, in order we to able to use Retrofit 2.0, we have you compile the following libraries in your dependencies build.gradle (Module: app) :
// retrofit 2.0 compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.retrofit2:converter-gson:2.0.2'
Add uses-permission in your AndroidManifest.xml like the following permission :
<uses-permission android:name="android.permission.INTERNET"/>
IMPLEMENTATION
First, create an interface that define Method GET of Retrofit that we'll use to get data profile. Give it name as ApiService.java. Paste the following code :
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
package com.example.putuguna.retrofit2.apis; | |
import com.example.putuguna.retrofit2.models.Profile; | |
import retrofit2.Call; | |
import retrofit2.http.GET; | |
import retrofit2.http.Query; | |
/** | |
* Created by putuguna on 14/06/16. | |
*/ | |
public interface ApiService { | |
@GET("myself") | |
Call<Profile> getMyProfile(@Query("id") String id); | |
} |
Second, create new java class, give it name as LoggingInterceptor.java. Paste the following code :
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
package com.example.putuguna.retrofit2.apis; | |
import android.util.Log; | |
import java.io.IOException; | |
import okhttp3.Interceptor; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.ResponseBody; | |
import okio.Buffer; | |
/** | |
* Created by putuguna on 1/27/2016. | |
*/ | |
public class LoggingInterceptor implements Interceptor { | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
long t1 = System.nanoTime(); | |
String requestLog = String.format("Sending request %s on %s%n%s", | |
request.url(), chain.connection(), request.headers()); | |
//YLog.d(String.format("Sending request %s on %s%n%s", | |
// request.url(), chain.connection(), request.headers())); | |
if (request.method().compareToIgnoreCase("post") == 0) { | |
requestLog = "\n" + requestLog + "\n" + bodyToString(request); | |
} | |
Log.d("TAG", "request" + "\n" + requestLog); | |
Response response = chain.proceed(request); | |
long t2 = System.nanoTime(); | |
String responseLog = String.format("Received response for %s in %.1fms%n%s", | |
response.request().url(), (t2 - t1) / 1e6d, response.headers()); | |
String bodyString = response.body().string(); | |
Log.d("TAG", "response" + "\n" + responseLog + "\n" + bodyString); | |
return response.newBuilder() | |
.body(ResponseBody.create(response.body().contentType(), bodyString)) | |
.build(); | |
} | |
public static String bodyToString(final Request request) { | |
try { | |
final Request copy = request.newBuilder().build(); | |
final Buffer buffer = new Buffer(); | |
copy.body().writeTo(buffer); | |
return buffer.readUtf8(); | |
} catch (final IOException e) { | |
return "did not work"; | |
} | |
} | |
} |
Third, create new java class, give it name as ApiClient.java, in this class we define the URL, set the LoggingInterceptor and client. Paste the following code :
Forth, create new java class, give it name as ProfileModel.java. This class will used to store the response of the JSON. Paste the following code :
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
package com.example.putuguna.retrofit2.models; | |
import com.google.gson.annotations.SerializedName; | |
/** | |
* Created by putuguna on 14/06/16. | |
*/ | |
public class Profile { | |
@SerializedName("id") | |
private String id; | |
@SerializedName("name") | |
private String name; | |
@SerializedName("job") | |
private String job; | |
@SerializedName("age") | |
private String age; | |
public Profile(String id, String name, String job, String age) { | |
this.id = id; | |
this.name = name; | |
this.job = job; | |
this.age = age; | |
} | |
public Profile() { | |
} | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getJob() { | |
return job; | |
} | |
public void setJob(String job) { | |
this.job = job; | |
} | |
public String getAge() { | |
return age; | |
} | |
public void setAge(String age) { | |
this.age = age; | |
} | |
} |
Last, create layout and java class of MainActivity.
activity_main.xml, paste the following code :
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"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.example.putuguna.retrofit2.MainActivity"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:id="@+id/hello" | |
android:text="Hello there, i trying to get my profile using Retrofit 2" /> | |
<Button | |
android:layout_below="@+id/hello" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/getprofile" | |
android:text="Get Profile"/> | |
<LinearLayout | |
android:gravity="center" | |
android:layout_marginTop="20dp" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_below="@+id/getprofile" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/idprofile"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/nameprofle"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/jobprofile"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/ageprofle"/> | |
</LinearLayout> | |
</RelativeLayout> |
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
package com.example.putuguna.retrofit2; | |
import android.app.ProgressDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.example.putuguna.retrofit2.apis.ApiClient; | |
import com.example.putuguna.retrofit2.apis.ApiService; | |
import com.example.putuguna.retrofit2.models.Profile; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
public class MainActivity extends AppCompatActivity { | |
private Button getData; | |
private TextView id; | |
private TextView name; | |
private TextView job; | |
private TextView age; | |
private ProgressDialog progressDialog; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
getData = (Button) findViewById(R.id.getprofile); | |
id = (TextView) findViewById(R.id.idprofile); | |
name = (TextView) findViewById(R.id.nameprofle); | |
job = (TextView) findViewById(R.id.jobprofile); | |
age = (TextView) findViewById(R.id.ageprofle); | |
getData.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
progressDialog = new ProgressDialog(MainActivity.this); | |
progressDialog.setTitle("Get My Prole"); | |
progressDialog.setMessage("Loading ..."); | |
progressDialog.show(); | |
getProfile(); | |
} | |
}); | |
} | |
private void getProfile(){ | |
ApiService apiService = ApiClient.getClient().create(ApiService.class); | |
Call<Profile> call = apiService.getMyProfile("1"); | |
call.enqueue(new Callback<Profile>() { | |
@Override | |
public void onResponse(Call<Profile> call, Response<Profile> response) { | |
progressDialog.dismiss(); | |
Profile p = response.body(); | |
id.setText(p.getId()); | |
name.setText(p.getName()); | |
job.setText(p.getJob()); | |
age.setText(p.getAge()); | |
} | |
@Override | |
public void onFailure(Call<Profile> call, Throwable t) { | |
progressDialog.dismiss(); | |
Toast.makeText(MainActivity.this, "Failed to load", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
} |
EmoticonEmoticon