際際滷

際際滷Share a Scribd company logo
Working with API
丼仂 舒从仂亠 Retrofit
Retrofit (仂亞仍舒仆仂 仂亳亳舒仍仆仂仄 舒亶)  亳仗仂弍亠亰仂仗舒仆亶
HTTP-从仍亳亠仆 亟仍 Android 亳 Java. 仆 磦仍磳
仆亠亰舒仄亠仆亳仄仄 亳仆仄亠仆仂仄 亟仍 舒弍仂  API 于 从仍亳亠仆-
亠于亠仆 仗亳仍仂亢亠仆亳. 舒从亳-仂 仍亠 5 仆舒亰舒亟 Android-
舒亰舒弍仂亳从舒仄 亟仍 舒弍仂  亠 仗亳仂亟亳仍仂 于仂仂亳
亞仂 从仂亟舒  仂弍舒仆仄亳 于亰仂于舒仄亳, AsyncTask'舒仄亳 亳
仗仂亳仄亳 束仆亳亰从仂仂于仆亠于仄亳損 于亠舒仄亳.  从仂仄仗舒仆亳 Square
于仗亳仍舒 舒从 亰舒仄亠舒亠仍仆 弍亳弍仍亳仂亠从  Retrofit.
仂弍舒于仍磳仄 于 build.gradle
compile 'com.squareup.retrofit2:retrofit:2.3.0'
 亢亟亠仄
舒舒亠仄 Retrofit 亳 OkHttp
舒亰舒  endpoint
- 弌仂亰亟舒亠仄 从仍舒 亳仗舒 Interface 亳 仂仗亳于舒亠仄 于亠 仆舒亳
仆亟仗仂亳仆
public interface Endpoint { }
亳仄亠 仆舒 Kotlin
个仂仄舒 亠仆亟仗仂亳仆仂于
仍亳 仆亢仆仂 亟仂弍舒于亳 亟亳仆舒仄亳亠从亳 仂仄亳仂于舒 仍 仂
亳仗仂仍亰亠仄 Path
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);
仍亳 仆亢仆仂 于舒于仍 仗舒舒仄亠 于 Get 亰舒仗仂 亳仗舒
?sort=desc 仂
@Query("sort") String sort
仍亳 仗亳仂从 仗舒舒仄亠仂于 query 亟亳仆舒仄亳亠仆 仂 亳仗仂仍亰亠仄
@QueryMap Map<String, String> options
丐舒从亢亠 仄 仄仂亢亠仄 仂仗舒于仍 仂弍亠从 从舒从 亠仍仂 亰舒仗仂舒.
@POST("users/new")
Call<User> createUser(@Body User user);
丐舒从亢亠 POST 亰舒仗仂 亟仂仍亢仆 弍 于 仂仄舒亠 Form-encoded
@FormUrlEncoded
@POST("user/edit")
Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);
仍亳 亢亠 仄 亟仂弍舒于仍磳仄 从舒仂仄仆亠 仂弍亠从 于 于亳亟亠 仂仂从 亳仍亳
舒亶仍仂于 仂 仆亠仂弍仂亟亳仄仂 亳仗仂仍亰仂于舒 MultiPart
@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody
description);
ApiClient.get().groupList(userId).enqueue(object: Callback<List<User>> {
override fun onResponse(call: Call<ListDayWeather>?, response:
Response<ListDayWeather>) {
if (response.isSuccessful) {
// viewInterface?.showUserList(response.body)
} else {
// viewInterface?.showAlert()
}
}
override fun onFailure(call: Call<ListDayWeather>?, t: Throwable?) {
viewInterface?.showNoInternet()
}
})
亳仄亠 亰舒仗仂舒.
亢仆仂 于 从舒亢亟亶 亰舒仗仂
亟仂弍舒于亳 亠亟亠?
丐舒仄 亞亟亠 弍亳仍亟亳仄 http 从仍亳亠仆 亟仂弍舒于仍磳仄 仍亠亟ム亠亠
okBuilder.addInterceptor { chain ->
val original = chain.request()
val request = original.newBuilder()
.header("lang", Storage.get().getLang())
.method(original.method(), original.body())
.build()
chain.proceed(request)
}
仍亳 亠仍亳 仆亢仆仂 仗仂于仂亳 亰舒仗仂
仗仂仍亠 仆亠仗亠仆仂亞仂 于亰仂于舒.
public static <T> void enqueueWithRetry(Call<T> call, final Callback<T> callback, MainActivityInterface
activityInterface) {
call.enqueue(new RetryableCallback<T>(call, activityInterface) {
@Override
public void onFinalResponse(Call<T> call, Response<T> response) {
if (activityInterface != null) { activityInterface.onSuccess(); }
callback.onResponse(call, response);
}
@Override
public void onFinalFailure(Call<T> call, Throwable t) {
if (activityInterface != null) { activityInterface.onErrorAfterRetry(); }
callback.onFailure(call, t);
}
});
}
 于亰于舒 仗亳 亰舒仗仂亠
ApiClient.get().groupList(userId).enqueue
仄亠仂 enqueue enqueueWithRetry
弌舒仄 RetryableCallback
public abstract class RetryableCallback<T> implements Callback<T> {
private int totalRetries = 5;
private static final String TAG = RetryableCallback.class.getSimpleName();
private final Call<T> call;
private int retryCount = 0;
MainActivityInterface activityInterface;
public RetryableCallback(Call<T> call, MainActivityInterface activityInterface) {
this.call = call;
this.activityInterface = activityInterface;
}
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (!isCallSuccess(response)) {
if (retryCount++ < totalRetries) {
Log.v(TAG, "Retrying API Call - (" + retryCount + " / " + totalRetries + ")");
if (activityInterface != null) {
activityInterface.onErrorInRetry();
}
retry();
} else {
onFinalResponse(call, response);
}
} else {
onFinalResponse(call, response);
}
}
@Override
public void onFailure(Call<T> call, Throwable t) {
Log.e(TAG, t.getMessage());
if (retryCount++ < totalRetries) {
Log.v(TAG, "Retrying API Call - (" + retryCount + " / " + totalRetries + ")");
if (activityInterface != null) {
activityInterface.onErrorInRetry();
}
retry();
} else {
onFinalFailure(call, t);
}
}
public void onFinalResponse(Call<T> call, Response<T> response) { }
public void onFinalFailure(Call<T> call, Throwable t) { }
private void retry() { call.clone().enqueue(this); }
public static boolean isCallSuccess(Response response) {
int code = response.code();
return (code >= 200 && code < 400);
}
}
弌仍从亳
http://square.github.io/retrofit/
https://blog.mindorks.com/android-development-best-
practices-83c94b027fd3
 舒 于亟仂仆仂于亳于舒 仄亠仆.
亠仄 仗舒亳弍仂 !

More Related Content

Working with API

  • 2. 丼仂 舒从仂亠 Retrofit Retrofit (仂亞仍舒仆仂 仂亳亳舒仍仆仂仄 舒亶) 亳仗仂弍亠亰仂仗舒仆亶 HTTP-从仍亳亠仆 亟仍 Android 亳 Java. 仆 磦仍磳 仆亠亰舒仄亠仆亳仄仄 亳仆仄亠仆仂仄 亟仍 舒弍仂 API 于 从仍亳亠仆- 亠于亠仆 仗亳仍仂亢亠仆亳. 舒从亳-仂 仍亠 5 仆舒亰舒亟 Android- 舒亰舒弍仂亳从舒仄 亟仍 舒弍仂 亠 仗亳仂亟亳仍仂 于仂仂亳 亞仂 从仂亟舒 仂弍舒仆仄亳 于亰仂于舒仄亳, AsyncTask'舒仄亳 亳 仗仂亳仄亳 束仆亳亰从仂仂于仆亠于仄亳損 于亠舒仄亳. 从仂仄仗舒仆亳 Square 于仗亳仍舒 舒从 亰舒仄亠舒亠仍仆 弍亳弍仍亳仂亠从 Retrofit.
  • 3. 仂弍舒于仍磳仄 于 build.gradle compile 'com.squareup.retrofit2:retrofit:2.3.0' 亢亟亠仄 舒舒亠仄 Retrofit 亳 OkHttp
  • 4. 舒亰舒 endpoint - 弌仂亰亟舒亠仄 从仍舒 亳仗舒 Interface 亳 仂仗亳于舒亠仄 于亠 仆舒亳 仆亟仗仂亳仆 public interface Endpoint { } 亳仄亠 仆舒 Kotlin
  • 5. 个仂仄舒 亠仆亟仗仂亳仆仂于 仍亳 仆亢仆仂 亟仂弍舒于亳 亟亳仆舒仄亳亠从亳 仂仄亳仂于舒 仍 仂 亳仗仂仍亰亠仄 Path @GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId); 仍亳 仆亢仆仂 于舒于仍 仗舒舒仄亠 于 Get 亰舒仗仂 亳仗舒 ?sort=desc 仂 @Query("sort") String sort 仍亳 仗亳仂从 仗舒舒仄亠仂于 query 亟亳仆舒仄亳亠仆 仂 亳仗仂仍亰亠仄 @QueryMap Map<String, String> options
  • 6. 丐舒从亢亠 仄 仄仂亢亠仄 仂仗舒于仍 仂弍亠从 从舒从 亠仍仂 亰舒仗仂舒. @POST("users/new") Call<User> createUser(@Body User user); 丐舒从亢亠 POST 亰舒仗仂 亟仂仍亢仆 弍 于 仂仄舒亠 Form-encoded @FormUrlEncoded @POST("user/edit") Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last); 仍亳 亢亠 仄 亟仂弍舒于仍磳仄 从舒仂仄仆亠 仂弍亠从 于 于亳亟亠 仂仂从 亳仍亳 舒亶仍仂于 仂 仆亠仂弍仂亟亳仄仂 亳仗仂仍亰仂于舒 MultiPart @Multipart @PUT("user/photo") Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);
  • 7. ApiClient.get().groupList(userId).enqueue(object: Callback<List<User>> { override fun onResponse(call: Call<ListDayWeather>?, response: Response<ListDayWeather>) { if (response.isSuccessful) { // viewInterface?.showUserList(response.body) } else { // viewInterface?.showAlert() } } override fun onFailure(call: Call<ListDayWeather>?, t: Throwable?) { viewInterface?.showNoInternet() } }) 亳仄亠 亰舒仗仂舒.
  • 8. 亢仆仂 于 从舒亢亟亶 亰舒仗仂 亟仂弍舒于亳 亠亟亠? 丐舒仄 亞亟亠 弍亳仍亟亳仄 http 从仍亳亠仆 亟仂弍舒于仍磳仄 仍亠亟ム亠亠 okBuilder.addInterceptor { chain -> val original = chain.request() val request = original.newBuilder() .header("lang", Storage.get().getLang()) .method(original.method(), original.body()) .build() chain.proceed(request) }
  • 9. 仍亳 亠仍亳 仆亢仆仂 仗仂于仂亳 亰舒仗仂 仗仂仍亠 仆亠仗亠仆仂亞仂 于亰仂于舒. public static <T> void enqueueWithRetry(Call<T> call, final Callback<T> callback, MainActivityInterface activityInterface) { call.enqueue(new RetryableCallback<T>(call, activityInterface) { @Override public void onFinalResponse(Call<T> call, Response<T> response) { if (activityInterface != null) { activityInterface.onSuccess(); } callback.onResponse(call, response); } @Override public void onFinalFailure(Call<T> call, Throwable t) { if (activityInterface != null) { activityInterface.onErrorAfterRetry(); } callback.onFailure(call, t); } }); } 于亰于舒 仗亳 亰舒仗仂亠 ApiClient.get().groupList(userId).enqueue 仄亠仂 enqueue enqueueWithRetry
  • 10. 弌舒仄 RetryableCallback public abstract class RetryableCallback<T> implements Callback<T> { private int totalRetries = 5; private static final String TAG = RetryableCallback.class.getSimpleName(); private final Call<T> call; private int retryCount = 0; MainActivityInterface activityInterface; public RetryableCallback(Call<T> call, MainActivityInterface activityInterface) { this.call = call; this.activityInterface = activityInterface; } @Override public void onResponse(Call<T> call, Response<T> response) { if (!isCallSuccess(response)) { if (retryCount++ < totalRetries) { Log.v(TAG, "Retrying API Call - (" + retryCount + " / " + totalRetries + ")"); if (activityInterface != null) { activityInterface.onErrorInRetry(); } retry(); } else { onFinalResponse(call, response); }
  • 11. } else { onFinalResponse(call, response); } } @Override public void onFailure(Call<T> call, Throwable t) { Log.e(TAG, t.getMessage()); if (retryCount++ < totalRetries) { Log.v(TAG, "Retrying API Call - (" + retryCount + " / " + totalRetries + ")"); if (activityInterface != null) { activityInterface.onErrorInRetry(); } retry(); } else { onFinalFailure(call, t); } } public void onFinalResponse(Call<T> call, Response<T> response) { } public void onFinalFailure(Call<T> call, Throwable t) { } private void retry() { call.clone().enqueue(this); } public static boolean isCallSuccess(Response response) { int code = response.code(); return (code >= 200 && code < 400); } }