In my android app. I am using retrofit2. I want to get json details from the api. The response is coming but the json objects are showing as null.
I am getting the json objects size. But i did not getting the values. How to parse the json from response.body
.
I want to get the value of DATA_TITLE
,DATA_IMAGE
, DATA
.
I am first time using retrofit. I have tried different methods. Please help me.
My json code is shown below. And the all source codes are also shown in below.
[{
"ID": {
"Tag": "string",
"Method": 5,
"Value": "68613",
"status": true
},
"DATA_TITLE": {
"Tag": "string",
"Method": 5,
"Value": "Hai aaaaaaa",
"status": true
},
"DATA_IMAGE": {
"Tag": "string",
"Method": 5,
"Value": "http://xxxxxx.com/desktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg",
"status": true
},
"DATA": {
"Tag": "string",
"Method": 5,
"Value": "",
"status": true
}
}, {
"ID": {
"Tag": "string",
"Method": 5,
"Value": "68613",
"status": true
},
"DATA_TITLE": {
"Tag": "string",
"Method": 5,
"Value": "Hai aaaaaaa",
"status": true
},
"DATA_IMAGE": {
"Tag": "string",
"Method": 5,
"Value": "http://xxxxxx.com/desktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg",
"status": true
},
"DATA": {
"Tag": "string",
"Method": 5,
"Value": "",
"status": true
}
}
]
And my gradle code is
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<List<Objects>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<List<DataResponse>> call =apiService.getDetails(Util.TemplateName,Util.p,Util.Handler,Util.AppName,Util.Type,Util.F);
call.enqueue(new Callback<List<DataResponse>>() {
@Override
public void onResponse(Call<List<DataResponse>> call, Response<List<DataResponse>> response) {
if (response.isSuccessful()) {
String json = gson.toJson(response.body());
List<DataResponse> dataResponses = response.body();
Log.d("LOG","Response size:"+dataResponses.size());
Log.d("LOG","Response data:"+dataResponses.get(0).getDataList());
// I want to get the value of DATA_TITLE,DATA_IMAGE, DATA
}
}
@Override
public void onFailure(Call<List<DataResponse>> call, Throwable t) {
Log.e("LOG","Response error:"+t);
}
});
}
}
ApiInterface
public interface ApiInterface {
@GET("getdetails")
Call<List<DataResponse>> getDetails(@Query("TemplateName") String TemplateName);
}
ApiClient.java
public class ApiClient {
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(Util.URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
DataResponse
public class DataResponse {
private List<Objects> results;
@SerializedName("")
@Expose
private List<Objects> dataList = new ArrayList<>();
public List<Objects> getDataList() {
return dataList;
}
public List<Objects> getResults() {
return results;
}
public void setResults(List<Objects> results) {
this.results = results;
}
}
Objects.java
public class Objects {
@SerializedName("ID")
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
>`