Получать данные с сервера, используя метод дооснащения - PullRequest
0 голосов
/ 30 апреля 2020

Я хочу получить данные с сервера, у меня есть API URL, например: https://example.com/PlanController/getData/2/7k Plan, int его api url 2 - это значение Dynami c и 7k План также является динамическим c Значение. Я хочу получить данные из метода модернизации. приведите несколько примеров.

public interface APIService {

    @GET("PlanController/getData")
    Call<CoachListResponse> getAllData(); 
}

Модернизированный клинт

 public class RetrofitClient {

private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl) {

    Gson gson = new GsonBuilder().setLenient().create();

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }
    return retrofit;
}
}

Ответы [ 2 ]

1 голос
/ 30 апреля 2020

Я хотел заменить только часть URL-адреса, и с этим решением мне не нужно передавать весь URL-адрес, только динамическую c часть и Ваш клиент Retrofit, так как нет необходимости изменять:

   public interface APIService {

      @GET("PlanController/getData/{value}/{plan}")
      Call<CoachListResponse> getAllData(@Path(value = "value", encoded = true) String value, @Path(value = "plan", encoded = true) String plan);
    }
1 голос
/ 30 апреля 2020

Определение сервиса после создания модификации

public interface APIService {

    @GET("getData/{id}/{kid}")
    Call<CoachListResponse> getAllData(@Path("id") Long id, @Path("kid") String kid); 
}
 public class RetrofitClient {

        private static APIService service;

        public static Retrofit getClient(String baseUrl) {
            Gson gson = new GsonBuilder().setLenient().create();

            if (retrofit == null) {
                retrofit = new Retrofit.Builder().baseUrl(baseUrl)
                        .addConverterFactory(GsonConverterFactory.create(gson)).build();
            }
            service = retrofit.create(APIService.class);
            return retrofit;
        }

        public static void getAllData(Callback<CoachListResponse> callback) {
            Call<CoachListResponse> regionsCall = service.getAllData();
            regionsCall.enqueue(callback);
        }
    }

, потребление

   RetrofitClient.getClient("https://example.com/PlanController/").getAllData(new Callback<CoachListResponse>() {
            @Override
            public void onResponse(Call<CoachListResponse> call, Response<CoachListResponse> response) {
                CoachListResponse responseDto = response.body();
              // logic
            }

            @Override
            public void onFailure(Call<CoachListResponse> call, Throwable t) {
               // logic
            }
        }, );
...