Skip to content

Instantly share code, notes, and snippets.

@coffeearmy
Created May 18, 2014 21:26
Show Gist options
  • Save coffeearmy/efce37377a605e5bf7ff to your computer and use it in GitHub Desktop.
Save coffeearmy/efce37377a605e5bf7ff to your computer and use it in GitHub Desktop.
Android Rest call with RetroFIT
//CALLL
SectionsResponse response= new SectionsResponse();
response.getSections(new Callback<Platforms>() {
@Override
public void success(Platforms arg0, Response arg1) {
Log.i("Result", arg0.getTotal_items()+"");
Toast.makeText(getBaseContext(), "HELLYEAH",Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError arg0) {
Toast.makeText(getBaseContext(), "OHNOES",Toast.LENGTH_LONG).show();
}
});
package com.coffeearmy.softtest.rest;
import retrofit.RestAdapter;
public class APIHandler {
private static final String API_URL = "http://api2.softonic.com";
private static RestAdapter restAdapter;
public APIHandler() {}
private static RestAdapter getRestAdapter(){
if(restAdapter==null){
restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
}
return restAdapter;
}
public static SoftonicAPI getApiInterface(){
// Create an instance of our API interface.
SoftonicAPI sfAPI=null;
try {
if(restAdapter==null){
restAdapter=getRestAdapter();
}
sfAPI = restAdapter.create(SoftonicAPI.class);
} catch (Exception e) {
e.printStackTrace();
}
return sfAPI;
}
}
public interface SoftonicAPI {
public enum Instance{
es, en, de, fr, it, br, pl, nl, jp, zh,
}
public enum Format{
xml,json
}
//http://api2.softonic.com/en/section_getPlatforms/2.json?key=API_KEY&results=2
@GET("/{instance}/section_getPlatforms/2.{format}")
void getSection(@Path("instance") Instance instance,
@Path("format") Format format,
@Query("key") String API_KEY,
Callback<Platforms> callback);
}
public class SectionsResponse {
private static final String API_KEY = ":D";
private Instance mInstance;
private Format mFormat;
public SectionsResponse() {
// Default Value
mInstance = SoftonicAPI.Instance.en;
mFormat = SoftonicAPI.Format.json;
}
public void getSections(Callback<Platforms> callback) {
// Create an instance of our API interface.
SoftonicAPI responseAPI = APIHandler.getApiInterface();
// Fetch and print a list of the contributors to this library.
responseAPI.getSection(mInstance, mFormat, API_KEY, callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment