Last active
February 14, 2018 15:05
-
-
Save jitendra3109/0908f01bf0e5b229b6bb78bd1a32854e to your computer and use it in GitHub Desktop.
Retrofit — Getting Started and Creating an Android Client
This file contains hidden or 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.jsroyal.githubrepo; | |
import java.util.List; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.http.GET; | |
import retrofit2.http.Path; | |
/** | |
* Created by jsroyal on 8/4/17. | |
*/ | |
public interface GitHubClient { | |
@GET("/users/{user}/repos") | |
Call<List<GitHubRepo>> reposForUser( | |
@Path("user") String user | |
); | |
} |
This file contains hidden or 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.jsroyal.githubrepo; | |
/** | |
* Created by jsroyal on 8/4/17. | |
*/ | |
public class GitHubRepo { | |
private int id; | |
private String name; | |
public GitHubRepo() { | |
} | |
public int getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
This file contains hidden or 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.jsroyal.githubrepo; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
import java.util.List; | |
/** | |
* Created by jsroyal on 8/4/17. | |
*/ | |
public class GitHubRepoAdapter extends ArrayAdapter<GitHubRepo> { | |
private Context context; | |
private List<GitHubRepo> values; | |
public GitHubRepoAdapter(Context context, List<GitHubRepo> values) { | |
super(context, R.layout.list_item_pagination, values); | |
this.context = context; | |
this.values = values; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View row = convertView; | |
if (row == null) { | |
LayoutInflater inflater = | |
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
row = inflater.inflate(R.layout.list_item_pagination, parent, false); | |
} | |
TextView textView = (TextView) row.findViewById(R.id.list_item_pagination_text); | |
GitHubRepo item = values.get(position); | |
String message = item.getName(); | |
textView.setText(message); | |
return row; | |
} | |
} |
This file contains hidden or 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"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" > | |
<TextView | |
android:id="@+id/list_item_pagination_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@+id/pagination_list" | |
android:textSize="20sp" > | |
</TextView> | |
</LinearLayout> |
This file contains hidden or 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:id="@+id/activity_main" | |
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.jsroyal.githubrepo.MainActivity"> | |
<ListView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/list_item"/> | |
</RelativeLayout> |
This file contains hidden or 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.jsroyal.githubrepo; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import java.util.List; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class MainActivity extends AppCompatActivity { | |
private ListView listView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
listView = (ListView) findViewById(R.id.list_item); | |
Retrofit.Builder builder = new Retrofit.Builder() | |
.baseUrl("https://api.github.com/") | |
.addConverterFactory(GsonConverterFactory.create()); | |
Retrofit retrofit = builder.build(); | |
GitHubClient client = retrofit.create(GitHubClient.class); | |
Call<List<GitHubRepo>> call = client.reposForUser("jsroyal"); | |
call.enqueue(new Callback<List<GitHubRepo>>() { | |
@Override | |
public void onResponse(Call<List<GitHubRepo>> call, Response<List<GitHubRepo>> response) { | |
List<GitHubRepo> repos = response.body(); | |
listView.setAdapter(new GitHubRepoAdapter(MainActivity.this, repos)); | |
} | |
@Override | |
public void onFailure(Call<List<GitHubRepo>> call, Throwable t) { | |
Toast.makeText(MainActivity.this, "error :(", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment