json에 대한 초보자이고 개조를하고 서버에서 내 목록으로 json 파일을 가져오고 싶습니다. 객체의 역 직렬화에 실패했습니다. 목록이 있습니다. Gson이 retrofit2를 사용하여 데이터를 가져 오는 경우
은 내가 질문 클래스public class Question {
private String label;
private ArrayList<String> question;
public Question(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void setQuestion(ArrayList<String> question) {
this.question = question;
}
public ArrayList<String> getQuestion() {
return question;
}
@Override
public String toString() {
String s = "Label: " + label;
for (String t : question) {
s += "\n" + t;
}
return s;
}
}
내가 개체를 얻을 수있는 API를 설정하는 Retrofit2를 사용에게이 여기
public interface ServiceApi {
@GET("/bins/{id}?pretty=1")
Call<Object> getJson(@Path("id") String id);
}
는 인터페이스를 구현하는 내 DataTask입니다
public class DataTask {
private static final String API_BASE_URL = "https://api.myjson.com";
private static final String TAG = "DataTask";
private static ServiceApi serviceApi;
public DataTask() {
createApi();
}
private void createApi() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient.build()).build();
serviceApi = retrofit.create(ServiceApi.class);
}
public static String getJson(String id) {
String t = null;
Call<Object> response = serviceApi.getJson(id);
try {
t = response.execute().body().toString();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
Log.d(TAG, "GET Success: " + t);
return t;
}
}
여기 내 MainActicity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<Question>[] questions = new List[1];
final String[] s = new String[1];
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
new DataTask();
s[0] = DataTask.getJson("2yf1q");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
questions[0] = new Gson().fromJson(s[0], new TypeToken<List<Question>>() {
}.getType());
Log.d(TAG, String.valueOf(questions[0].toString()));
for (Question q : questions[0]) {
Log.d(TAG, q.toString());
}
}
}.execute();
}
}
입니다 그리고 여기가 내가 그것을 할 수있는 방법을 이해하지 않습니다이 예외
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 1 column 30 path $[0].question[1]
at com.google.gson.Gson.fromJson(Gson.java:902)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at svmc.anthao.MainActivity$1.onPostExecute(MainActivity.java:49)
at svmc.anthao.MainActivity$1.onPostExecute(MainActivity.java:36)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
을 가지고 시작하면 내 JSON 링크 https://api.myjson.com/bins/2yf1q?pretty=1
입니다. 고맙습니다. 고맙습니다.
이 –
당신을 감사하여 개조 호출의 반환 형식을 시도 @ 대답 FilipeEsperandio, 나는 시도했지만 동일한 예외가 발생 –