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: ");
}
});
}
}