티스토리 뷰
안녕하세요 오늘의 포스팅은
Use JsonReader.setLenient(true) to accept malformed JSON
요런 에러가 났을때 이야기 인데요.
아래 확인할 상황 및 해결 방법을 설명할께요.
1. Check
현재소스 Gradle 확인 retrofit2 2.1.0 -> 2.4.0으로 변경해서 저에러가 나왔는지를 확인해봐야합니다
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
2. Solution
Gradle 변경 및 추가 해주세요
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
compile 'com.squareup.okhttp3:okhttp:3.4.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.2'
- Source 추가 및 변경
MainActivity 추가 사항
public RetroClient retroClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);retroClient = RetroClient.getInstance(this).createBaseApi();
Check();
}
public void Check()
{
Toast.makeText(this, "GET 1 Clicked", Toast.LENGTH_SHORT).show();
retroClient.getFirst("1", new RetroCallback() {
@Override
public void onError(Throwable t) {
Log.e("onFailure", t.getMessage());
}
@Override
public void onSuccess(int code, Object receivedData) {
Log.d("onSuccess", String.valueOf(code));
}
@Override
public void onFailure(int code) {
Log.e("onFailure", String.valueOf(code));
}
});
}
ResponsResult.class
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ResponseResult {
@SerializedName("resultCode")
@Expose
private String resultCode;
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
}
RetroBaseApiService.interface
import com.duranno.intraapp.Model.ResponseResult;
import retrofit2.Call;
import retrofit2.http.GET;
public interface RetroBaseApiService {
final String Base_URL = "http://www.Result.com/Result/";
@GET("Result.asp")
Call<ResponseTag> getFirst();
}
RetroCallback.interface
public interface RetroCallback<T> {
void onError(Throwable t);
void onSuccess(int code, T receivedData);
void onFailure(int code);
}
RetorClient.class
import android.content.Context;
import com.duranno.intraapp.Interface.RetroBaseApiService;
import com.duranno.intraapp.Interface.RetroCallback;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.http.protocol.HttpRequestInterceptorList;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetroClient {
private RetroBaseApiService apiService;
public static String baseUrl = RetroBaseApiService.Base_URL;
private static Context mContext;
private static Retrofit retrofit;
private static Gson gson;
private static class SingletonHolder {
private static RetroClient INSTANCE = new RetroClient(mContext);
}
public static RetroClient getInstance(Context context) {
if (context != null) {
mContext = context;
}
return SingletonHolder.INSTANCE;
}
private RetroClient(Context context) {
gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(createOkHttpClient())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
public RetroClient createBaseApi() {
apiService = create(RetroBaseApiService.class);
return this;
}
/**
* create you ApiService
* Create an implementation of the API endpoints defined by the {@code service} interface.
*/
public <T> T create(final Class<T> service) {
if (service == null) {
throw new RuntimeException("Api service is null!");
}
return retrofit.create(service);
}
public void getFirst(String id, final RetroCallback callback) {
apiService.getFirst().enqueue(new Callback< ResponseResult >() {
@Override
public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
if (response.isSuccessful()) {
callback.onSuccess(response.code(), response.body());
} else {
callback.onFailure(response.code());
}
}
@Override
public void onFailure(Call< ResponseResult> call, Throwable t) {
callback.onError(t);
}
});
}
private static OkHttpClient createOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
return builder.build();
}
}
2.1.0 에서 2.4.0으로 추가된부분은 이부분인데요 저걸 추가함으로해서 Retro에서 알기 힘들었어 JSON파일을 잘가지고 왔는지 왜 에러인지를 알수있는 로그확인 기능이
생겼어요 . 이거 가지고 2틀이나 삽질을 했네요 에혀...
gson = new GsonBuilder()
.setLenient()
.create();retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(createOkHttpClient())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
private static OkHttpClient createOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
return builder.build();
}
보시고 이상하거나 고쳤으면 하는부분 댓글 부탁드려요~~~
'android' 카테고리의 다른 글
[android] Lock 화면 위에 Activity 띄우기 (0) | 2018.05.29 |
---|---|
[android] 8.0 OREO Notification 알림 대응하기 (2) | 2018.03.20 |
[android] encode decode (0) | 2018.02.21 |
[android] Notification 알림 제거 하지 않기 (0) | 2018.01.30 |
[Kotlin] Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter intent (1) | 2017.12.15 |
- Total
- Today
- Yesterday
- Swift button text underline
- firebase remote config
- [WindowScene] Failed to Error
- autoclosure
- Swift textField max length
- swift autoclosure
- Android
- Swift version code
- 위메프
- Swift app version check
- Swift label underline storyboard
- Sandbox account sign out
- 쇼미더머니6
- android notification
- Swift button underline programmatically
- 맛집
- Bundle main infodictionary swift 4
- Swift 앱 버전
- Sandbox account logout
- Swift 노치 구분
- wwdc21 actor
- iPhone X safe area size
- Swift check notch
- Sandbox test login on
- SceneDelegate error
- 샤브샤브
- swift actor
- swift uitextfield 글자수 제한
- [WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
- How to get version swift
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |