|
| 1 | +package com.hellohasan.tenthclass.Network; |
| 2 | + |
| 3 | +import com.hellohasan.tenthclass.RepositoryListShow.Repository; |
| 4 | +import com.hellohasan.tenthclass.UserInfoShow.User; |
| 5 | +import com.orhanobut.logger.Logger; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import retrofit2.Call; |
| 10 | +import retrofit2.Callback; |
| 11 | +import retrofit2.Response; |
| 12 | + |
| 13 | +public class NetworkCall implements GitHubApiService { |
| 14 | + |
| 15 | + @Override |
| 16 | + public void getUser(String userId, final ResponseCallback<User> callback) { |
| 17 | + |
| 18 | + RetrofitApiInterface retrofitApiInterface = RetrofitApiClient.getClient().create(RetrofitApiInterface.class); |
| 19 | + Call<User> call = retrofitApiInterface.fetchUserFromServer(userId); |
| 20 | + |
| 21 | + call.enqueue(new Callback<User>() { |
| 22 | + @Override |
| 23 | + public void onResponse(Call<User> call, Response<User> response) { |
| 24 | + Logger.d(response.raw()); |
| 25 | + if(response.code()==200) |
| 26 | + callback.onSuccess(response.body()); |
| 27 | + else |
| 28 | + callback.onError(new Exception(response.message())); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void onFailure(Call<User> call, Throwable t) { |
| 33 | + callback.onError(t); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void getRepositoryList(String userId, final ResponseCallback<List<Repository>> callback) { |
| 41 | + RetrofitApiInterface retrofitApiInterface = RetrofitApiClient.getClient().create(RetrofitApiInterface.class); |
| 42 | + Call<List<Repository>> call = retrofitApiInterface.fetchRepositoryListFromServer(userId); |
| 43 | + |
| 44 | + call.enqueue(new Callback<List<Repository>>() { |
| 45 | + @Override |
| 46 | + public void onResponse(Call<List<Repository>> call, Response<List<Repository>> response) { |
| 47 | + if(response.code()==200) |
| 48 | + callback.onSuccess(response.body()); |
| 49 | + else |
| 50 | + callback.onError(new Exception(response.message())); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onFailure(Call<List<Repository>> call, Throwable t) { |
| 55 | + callback.onError(t); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | +} |
0 commit comments