【发布时间】:2016-10-04 23:30:59
【问题描述】:
我只是 json 和改造的新手,想从服务器获取一个 json 文件到我的列表中。
我有 Question 课
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;
}
}
我使用Retrofit2设置一个Api来获取对象
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();
}
}
这是我的 Json 链接https://api.myjson.com/bins/2yf1q?pretty=1
当我启动时,我得到了这个异常
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)
我不明白怎么可能。 请帮我解决一下,非常感谢
【问题讨论】:
-
尝试让你的改造调用返回类型
-
谢谢@FilipeEsperandio 的回复,我试过了,但得到了同样的例外
标签: android json android-asynctask gson retrofit2