티스토리 뷰

반응형

안녕하세요 오늘의 포스팅은 


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();
}


보시고 이상하거나 고쳤으면 하는부분 댓글 부탁드려요~~~

반응형
댓글