Created
April 3, 2015 11:56
-
-
Save noder123/e53b715989919df7017c to your computer and use it in GitHub Desktop.
Using RetroFit in Android apps
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
import retrofit.RestAdapter; | |
/** | |
* Created by NODER on 3/4/2015. | |
*/ | |
public class ApiClient { | |
private static UserApiInterface userApiInterface; | |
public static RestAdapter getRestAdapter(){ | |
return DemoApplication.getInstance().getRestAdapter(); | |
} | |
public static UserApiInterface getUserApiInterface(){ | |
if(userApiInterface==null){ | |
userApiInterface = getRestAdapter().create(UserApiInterface.class); | |
} | |
return userApiInterface; | |
} | |
public interface ErrorCodes{ | |
public static final int ERR_CODE_UNAME_EXIST = 43; | |
public static final int ERR_CODE_EMAIL_EXIST = 44; | |
public static final int ERR_INTERNAL_SERVER = 500; | |
public static final int ERR_EMAIL_DOES_NOT_EXIST = 46; | |
} | |
} |
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
import android.app.Application; | |
/** | |
* Created by NODER on 3/4/2015. | |
*/ | |
public class DemoApplication extends Application { | |
private OkHttpClient okHttpClient; | |
private RestAdapter restAdapter; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
okHttpClient = new OkHttpClient(); | |
} | |
public OkHttpClient getOkHttpClient(){ | |
return okHttpClient; | |
} | |
public RestAdapter getRestAdapter(){ | |
if(restAdapter==null){ | |
restAdapter = new RestAdapter.Builder() | |
.setEndpoint("URL") | |
.build(); | |
} | |
return restAdapter; | |
} | |
} |
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
import retrofit.Callback; | |
import retrofit.RetrofitError; | |
import retrofit.http.Field; | |
import retrofit.http.FormUrlEncoded; | |
import retrofit.http.GET; | |
import retrofit.http.POST; | |
import retrofit.http.Path; | |
/** | |
* Created by NODER on 3/4/2015. | |
*/ | |
public interface UserApiInterface { | |
@GET("/user/search/{query}") | |
public void search(@Path("query") String query, Callback<String> cb); | |
@GET("/user/login") | |
public Response login(@Query("email") String email, @Query("password") String password); | |
} |
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
/** | |
* Created by NODER on 3/4/2015. | |
*/ | |
public class UserIntentService extends IntentService{ | |
public UploadService2() { | |
super(""); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
Response response = ApiClient.getUserApiInterface().login("email", "password"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment