Created
May 18, 2014 21:26
-
-
Save coffeearmy/efce37377a605e5bf7ff to your computer and use it in GitHub Desktop.
Android Rest call with RetroFIT
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
//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(); | |
} | |
}); |
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.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; | |
} | |
} |
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
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); | |
} |
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
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