Created
October 31, 2015 14:30
-
-
Save hachy/82d0f7f1a93d80b12fd8 to your computer and use it in GitHub Desktop.
Retrofit sample
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.retrofitsample; | |
import java.util.List; | |
import retrofit.Call; | |
import retrofit.http.GET; | |
import retrofit.http.Path; | |
public interface GitHubService { | |
@GET("/users/{user}/repos") | |
Call<List<Repo>> listRepos(@Path("user") String user); | |
} |
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.retrofitsample; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import java.util.ArrayList; | |
import java.util.List; | |
import retrofit.Call; | |
import retrofit.Callback; | |
import retrofit.GsonConverterFactory; | |
import retrofit.Response; | |
import retrofit.Retrofit; | |
public class MainActivity extends AppCompatActivity { | |
private ArrayList<String> items; | |
private ListView listView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
listView = (ListView) findViewById(R.id.list_view); | |
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl("https://api.github.com") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
GitHubService service = retrofit.create(GitHubService.class); | |
Call<List<Repo>> repos = service.listRepos("hachy"); | |
repos.enqueue(new Callback<List<Repo>>() { | |
@Override | |
public void onResponse(Response<List<Repo>> response, Retrofit retrofit) { | |
items = new ArrayList<>(); | |
for (Repo res : response.body()) { | |
items.add(res.getName()); | |
} | |
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, items); | |
listView.setAdapter(adapter); | |
} | |
@Override | |
public void onFailure(Throwable t) { | |
} | |
}); | |
} | |
} |
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.retrofitsample; | |
public class Repo { | |
public String name; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment