Android

Retrofit 통신

그란. 2019. 4. 19. 16:39

모델 생성

public class DeliveryModel {
    @SerializedName("MSG_ID")
    @Expose
    private String mSGID;
        
}

 

ApiService

public interface ApiService {

    @FormUrlEncoded
    @POST("lst/101")
    Call<ProductModel> getProduct(@Field("REQUEST") String request);

    @FormUrlEncoded
    @POST("lst/102")
    Call<DeliveryModel> getDelivery(@Field("REQUEST") String request);

    @FormUrlEncoded
    @POST("ins/103")
    Call<SetOrderModel> setOrder(@Field("REQUEST") String request);

    @FormUrlEncoded
    @POST("lst/104")
    Call<DiscountRateModel> getDCRate(@Field("REQUEST") String request);

}

 

 

 

ApiUtils

public class ApiUtils {

    public static final String BASE_URL = ""; //개발용
    public static final String BASE_URL_RELEASE = ""; //배포용

    public static ApiService getApiService() {
        return RetrofitClient.getClient(BASE_URL_RELEASE).create(ApiService.class);
    }

}

 

RetrofitClient

public class RetrofitClient {
    private static final String TAG = RetrofitClient.class.getSimpleName();

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

}

 

 

 apiService.setOrder(stringBuilder.toString()).enqueue(new Callback<SetOrderModel>() {
            @Override
            public void onResponse(Call<SetOrderModel> call, Response<SetOrderModel> response) {

                if (response.body().getODS0() == null) {
//                    FirebaseCrash.log("주문등록 실패 (입력오류)\n" + insert);
                    showToast("주문등록 실패(입력오류)");
                    System.out.println(stringBuilder.toString());
                    binding.btnRegiOrder.setEnabled(true);
                    return;
                }
          
            }

            @Override
            public void onFailure(Call<SetOrderModel> call, Throwable t) {
                binding.btnRegiOrder.setEnabled(true);
                runOnUiThread(() -> showToast("통신오류 (와이파이 연결을 확인 해주세요)"));

            }
        });