0

I have a server where I query to get a list of topics in a forum. The data returned via curl is something like this -

[
  {
    "id": 728,
    "date": "2016-01-01T13:01:51",
    "date_gmt": "2016-01-01T07:31:51",
    ....
  },
  {
    "id": 556,
    "date": "2015-06-07T21:16:59",
    "date_gmt": "2015-06-07T15:46:59",
    ....
  },
  {
    "id": 554,
    "date": "2015-06-07T21:16:28",
    "date_gmt": "2015-06-07T15:46:28",
    ....
  }
]

Here every JSONObject is a data about forum topic.

{
  "id": 554,
  "date": "2015-06-07T21:16:28",
  "date_gmt": "2015-06-07T15:46:28",
  ....
}

I want to display this data in a ListView in an Android app.

However, since I am using retrofit2 and gson to create the objects for the ListView, the response I always get is Not Found.

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(ENDPOINT)
        .addConverterFactory(buildGsonConverter())
        .build();

serverAPI = retrofit.create(ServerAPI.class);

private Converter.Factory buildGsonConverter() {
    return GsonConverterFactory.create();
}

Call<List<Forum>> call = App.serverAPI.getListOfForums();
call.enqueue(new Callback<List<Forum>>() {
    @Override
    public void onResponse(Call<List<Forum>> call, Response<List<Forum>> response) {
        Log.i(TAG, "onResponse: " + (null != response.message() ? response.message() : ""));
        Log.i(TAG, "response body - " + (null != response.body() ? response.body() : ""));
        if (response.isSuccessful()) {
            adapter.setForums(response.body());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(Call<List<Forum>> call, Throwable t) {
        Log.i(TAG, "onFailure: " + t.toString());
        Log.i(TAG, "onFailure: " + t.getMessage());
    }
});

ServerAPI.class - 

@GET("/forum/")
Call<List<Forum>> getListOfForums();

Right now I am not doing anything when deserializing the returned JSON. Even if I use a JsonDeserializer, how should I go about it such that it is easier to populate the List<Forum>.

1 Answer 1

1

You dont need to change JSON itself.

public interface ServerAPI {
   @GET("/forum")
   Call<Forum> getListOfForums();
 }

FormResponse.java

public class ForumResponse {
  @SerializedName("id")
  private int id;

  @SerializedName("date")
  private String date;

  @SerializedName("date_gmt")
  private String date_gmt;
}

onResponse in MainActivity.java

@Override
public void onResponse(Call<Forum> call, Response<Forum> response) {
    String jsonString = response.body().toString();
    Log.i("onResponse", jsonString);
    Type listType = new TypeToken<List<ForumResponse>>() {}.getType();
    List<ForumResponse> yourList = new Gson().fromJson(jsonString, listType);
    Log.i("onResponse", yourList.toString());
}
Sign up to request clarification or add additional context in comments.

Comments

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.