Reputation: 129
I am using a retrofit to get Json data but something is wrong. No data is being received.
The following is my model class
public class DataList {
@SerializedName("pariyojanaId")
@Expose
private Float pariyojanaId;
@SerializedName("projectStatus")
@Expose
private String projectStatus;
@SerializedName("heading")
@Expose
private String heading;
@SerializedName("projectNumber")
@Expose
private Float projectNumber;
@SerializedName("fieldTitle")
@Expose
private String fieldTitle;
@SerializedName("targetedGroup")
@Expose
private String targetedGroup;
//Getter Setter
}
My interface
public interface PariyojanaInterface {
@GET("projectBudget/pariyojanaListForMobile")
Call<List<DataList>> getData(
@Query("memberId") int id);
}
I have an API interface which is working. Below is my retrofit function.
apiInterface = ApiClient.getApiClient().create(PariyojanaInterface.class);
Call<List<DataList>> call = apiInterface.getData(id);
Log.e("url", call.request().toString());
call.enqueue(new Callback<List<DataList>>() {
@Override
public void onResponse(Call<List<DataList>> call, Response<List<DataList>> response) {
Log.e("url", "hello");
posts = response.body();
adapter = new PariyojanaAdapter(posts,getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<DataList>> call, Throwable t) {
Log.e("error",t.getMessage());
}
});
When i run this program, upto the line
Log.e("url", call.request().toString());
I am getting response but after that in line
call.enqueue(new Callback<List<DataList>>() {}
I think there is some problem. I am not able to run my program past this. Can anybody please help me.
Upvotes: 0
Views: 84
Reputation: 5017
you cant directly access the array without going through the outer object,you will need one more pojo class like this :
DataResponse.java
public class DataResponse {
@SerializedName("data")
@Expose
private List<DataList> data = null;
public List<DataList> getData() {
return data;
}
public void setData(List<DataList> data) {
this.data = data;
}
}
Change all List<DataList>
to DataResponse
i.e, convert your network call like this :
apiInterface = ApiClient.getApiClient().create(PariyojanaInterface.class);
Call<DataResponse> call = apiInterface.getData(id);
Log.e("url", call.request().toString());
call.enqueue(new Callback<DataResponse>() {
@Override
public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
Log.e("url", "hello");
ArrayList<DataList> post_list = new ArrayList<>(response.body().getData());
adapter = new PariyojanaAdapter(post_list,getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<DataResponse> call, Throwable t) {
Log.e("error",t.getMessage());
}
});
and change interface like this
public interface PariyojanaInterface {
@GET("projectBudget/pariyojanaListForMobile")
Call<DataResponse> getData(
@Query("memberId") int id);
}
As i have mentioned, your endpoint is returning an object, not an array. So specify an object as return params for your network callbacks
Upvotes: 1
Reputation: 1148
Try this flow
try {
JSONObject jsonObject = new JSONObject(resultJSONResponse);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject o = jsonArray.getJSONObject(i);
strid = o.getString("pariyojanaId");
strstatus = o.getString("projectStatus");
showMethod();
}
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 1