1

I am use Retrofit 2. I use test JSON on http://ip.jsontest.com/.It is very simple JSON. Why I am take this error?

In real project i have this ERROR too, but i think, it is because I have very big JSON. And I thy use test JSON. Need HELP))

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

This is JSON

{ "ip": "54.196.188.78" }

My Interface

public interface UmoriliApi {
    @GET(".")
    Call<List<Test>> getData();
}

My Test class

public class Test {
    @SerializedName("ip")
    @Expose
    private String ip;

    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
}

My API class

public class App extends Application {

    private static UmoriliApi umoriliApi;
    private Retrofit retrofit;

    @Override
    public void onCreate() {
        super.onCreate();

        retrofit = new Retrofit.Builder()
                .baseUrl("http://ip.jsontest.com/") 
                .addConverterFactory(GsonConverterFactory.create()) 
                .build();
        umoriliApi = retrofit.create(UmoriliApi.class); 
    }

    public static UmoriliApi getApi() {
        return umoriliApi;
    }
}

My MainActivity class

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "TAG";
    List<Test> posts;

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

        posts = new ArrayList<>();

        App.getApi().getData().enqueue(new Callback<List<Test>>() {
            @Override
            public void onResponse(Call<List<Test>> call, Response<List<Test>> response) {
                posts.addAll(response.body());
                Log.d(TAG, "onResponse: "+posts.size());
            }

            @Override
            public void onFailure(Call<List<Test>> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
            }
        });
    }
}
3
  • @GET(".") Call<Test> getData(); modified this code Commented Aug 10, 2017 at 18:57
  • @Akash Thank. But how i can get list of test? posts.addAll(response.body()); Commented Aug 10, 2017 at 19:09
  • @Sergey after change your retrofit interface to [@GET(".") Call<Test> getData();] in your case you can do posts.add(response.body()); Commented Dec 6, 2018 at 14:24

1 Answer 1

5

Basically you are expecting and Array but you received a JSON Object.

As Akash said in the comment:

Call<List<Test>> getData();

List<Test> is what you write when you expect and Array. You need to write Call<Test> for an object Test

You will also have to change the callback.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank. I changed @GET(".") Call<Test> getData(); How I can get List of Test?
@SergeyRokitskiy: Your JSON does not have a list. It has one JSON object. If you want a list, your Web service needs to return a list formatted in JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.