2017-04-22 4 views
1

Ich versuche Retrofit 2 zu verwenden, ein JSON-Array zu analysieren:Android Retrofit JSON Erwartete BEGIN_OBJECT Parsen aber war BEGIN_ARRAY

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

Hier ist die Haupttätigkeit meines Projekts:

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

Ich habe eine Klasse für den Zugriff auf meine Seite auf dem MAMP-Server erstellt:

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

Ich habe auch zwei Klassen für das Objekt, das ich versuche zu analysieren:

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

und schließlich meine Schnittstelle:

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

public interface Questionable { 

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

Wenn ich ausführen, um die Anwendung der folgende Fehler aufgetreten:

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

Ich suchte im Internet, aber ich kann keine Lösung für mein Problem finden ...

Antwort

1

Gson erwartet ein Objekt, aber es erhielt eine Array.

Dies ist eine einfache json Array mit 2-Objekten:

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

Dies ist ein Objekt, das eine Liste Fragen enthält, die die Fragen in ihm hat.

public class QuestionList { 

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

} 

Welche aussehen würde:

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

Also, wenn Sie Retrofit sagen, dass Sie erwarten ein Call<QuestionList> Gson die oben Json erwartet, die nicht die gleiche wie die oben ist. Es ist eine Liste, die in ein Objekt "eingewickelt" ist.


Ihr Anruf-Objekt sollte wie folgt aussehen:

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

von denen jeder eine aktuelle Liste/Array von Question.

+0

Vielen Dank, es funktioniert perfekt –

Verwandte Themen