Error with retrofit

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

public interface RestApi {

@GET("/posts")
Call<Model[]> getWheatherReport();

}

public class MainActivity extends AppCompatActivity {

String url = "http://jsonplaceholder.typicode.com/posts";
TextView txt_city, txt_status, txt_humidity, txt_pressure;

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

    txt_city = (TextView) findViewById(R.id.txt_city);
    txt_status = (TextView) findViewById(R.id.txt_status);
    txt_humidity = (TextView) findViewById(R.id.txt_humidity);
    txt_pressure = (TextView) findViewById(R.id.txt_press);


    getReport();
}

void getReport() {

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RestApi service = retrofit.create(RestApi.class);

    Call<Model[]> call = service.getWheatherReport();

    call.enqueue(new Callback<Model[]>() {
        @Override
        public void onResponse(Response<Model[]> response, Retrofit retrofit) {
            Log.v("CONSOLES", "--> "+response.body());
            try {

                Integer userId = response.body()[0].getUserId();

                Integer id = response.body()[0].getId();

                String title = response.body()[0].getTitle().toString();

                String body = response.body()[0].getBody().toString();

                txt_city.setText("city  :  " + userId);
                txt_status.setText("status  :  " + id);
                txt_humidity.setText("humidity  : " + title);
                txt_pressure.setText("pressure  :  " + body);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(Throwable t) {
            Log.v("CONSOLES-ERRO", "--> "+t.toString());
        }
    });
}

}

I've tried several tutorials and the error continues.

    
asked by anonymous 29.07.2016 / 18:15

1 answer

-1

Speak Roberto,

Your problem is in Model, to get it in array mode, you're doing it wrong in Retrofit:

It would have to be:

Callback<List<Model>>

instead of:

Callback<Model[]>

Hugs.

    
29.07.2016 / 18:32