2017-04-22 74 views
1

我试图用改造2解析JSON数组:Android的改造解析JSON预计BEGIN_OBJECT但BEGIN_ARRAY

[{"question":"question 1"},{"question":"question 2"}]

这里是我的项目的主要活动:

package maxime.mysqltest; 

import android.graphics.Movie; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 

import java.util.List; 

import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 

public class MainActivity extends AppCompatActivity { 
    private static final String TAG = MainActivity.class.getSimpleName(); 

    private final static int idForm = 1; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Questionable questionService = 
       ApiClient.getClient().create(Questionable.class); 

     Call<QuestionList> call = questionService.getQuestions(idForm); 
     call.enqueue(new Callback<QuestionList>() { 
      @Override 
      public void onResponse(Call<QuestionList>call, Response<QuestionList> response) { 
       List<Question> questions = response.body().getQuestions(); 
       Log.d(TAG, "Number of questions received: " + questions.size()); 
      } 

      @Override 
      public void onFailure(Call<QuestionList>call, Throwable t) { 
       // Log error here since request failed 
       Log.e(TAG, t.toString()); 
      } 
     }); 
    } 
} 

我创建了一个类来访问MAMP服务器上的我的页面:

public class ApiClient { 

    public static final String BASE_URL = "http://*myIP*:8888"; 
    private static Retrofit retrofit = null; 


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

我也有两个类的对象,我尝试解析:

public class Question { 

    private final String question; 


    public Question(String question) 
    { 
     this.question=question; 
    } 

} 

public class QuestionList { 

    private List<Question> questions = new ArrayList<>(); 

    public QuestionList(List<Question> questions) 
    { 
     this.questions=questions; 
    } 

    public List<Question> getQuestions() { 
     return questions; 
    } 
} 

,最后我的界面:

import retrofit2.Call; 
import retrofit2.http.GET; 
import retrofit2.http.Query; 

public interface Questionable { 

    @GET("/takeQuestions.php") 
    Call<QuestionList> getQuestions(@Query("idForm") int idForm); 
} 

当我运行应用程序下面的错误出现:

E/MainActivity: com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was 
BEGIN_ARRAY at line 1 column 2 path $ 

我在互联网上搜索,但我找不到解决我的问题...

回答

1

Gson预计对象但它收到阵列

这是一个纯JSON阵列2个对象:

[{"question":"question 1"},{"question":"question 2"}] 

这是一个对象包含列表问题具有在它的问题。

public class QuestionList { 

    private List<Question> questions = new ArrayList<>(); 

} 

将目光像:

{ 
    "questions" : [{"question":"question 1"},{"question":"question 2"}] 
} 

所以,当你告诉改造,你期待一个Call<QuestionList> GSON预计,JSON,这是不一样的顶部的一个以上。它是一个“包裹”在一个物体内的列表。


您的通话对象应该是这样的:

Call<Question[]> getQuestions() 
    // or 
Call<List<Question>> getQuestions() 

上述的每一种的Question的实际列表/阵列。

+0

非常感谢,它完美的作品 –

相关问题