I want to fetch json from [this link][1]: https://api.myjson.com/bins/38ln5 using retrofit
sample json is
{
\"students\": [
{
\"id\": \"
You can access both JSONArray and JSONObject using retrofit 2.0 in android. Here you want to access JSONArray using retrofit for which you should do following:
Interface Class:
package com.androidtutorialpoint.retrofitandroid;
import java.util.List;
import retrofit.Call;
import retrofit.http.GET;
/**
* Created by navneet on 4/6/16.
*/
public interface RetrofitArrayAPI {
/*
* Retrofit get annotation with our URL
* And our method that will return us details of student.
*/
@GET("api/RetrofitAndroidArrayResponse")
Call> getStudentDetails();
}
Above is the interface. Now to display JSONArray data on your smartphone screen call following function:
void getRetrofitArray() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);
Call> call = service.getStudentDetails();
call.enqueue(new Callback>() {
@Override
public void onResponse(Response> response, Retrofit retrofit) {
try {
List StudentData = response.body();
for (int i = 0; i
My POJO Class was following:
package com.androidtutorialpoint.retrofitandroid;
public class Student {
//Variables that are in our json
private int StudentId;
private String StudentName;
private String StudentMarks;
private int inStock;
//Getters and setters
public int getStudentId() {
return StudentId;
}
public void setStudentId(int bookId) {
this.StudentId = StudentId;
}
public String getStudentName() {
return StudentName;
}
public void setStudentName(String name) {
this.StudentName = StudentName;
}
public String getStudentMarks() {
return StudentMarks;
}
public void setStudentMarks(String price) {
this.StudentMarks = StudentMarks;
}
}
So in this way, you will be able to capture JSONArray from URL using Retrofit 2.0 and display that data on your screen.
I hope I am able to answer your query.
Credits: I read this tutorial on: AndroidTutorialPoint for Retrofit 2.0