Bruno
Bruno

Reputation: 319

Get a Json array with Retrofit

Hi I'm trying to parse into a List the following JSON response

{"beers":[{"id":"1","nombre":"Larzobispa","localidad":"Alcorcon","provincia":"Madrid"},{"id":"2","nombre":"La Cibeles","localidad":"Madrid","provincia":"Madrid"}]}

Im using the following code

My interface

public interface AbebeerApi {
    @GET("/beer_app/beers")
    List<SingleBeer> listBeers();
}

Implementation of the AbebeerApi

Retrofit retrofit = new Retrofit.Builder()
     .baseUrl("end_point_url")
     .build();

AbebeerApi service = retrofit.create(AbebeerApi.class);

And the call for the created service

List<SingleBeer> beers = service.listBeers();

I dont get any response in beers list. My app close and I get no information in the logcat.

This is my POJO class

public class SingleBeer {
    int id;
    String nombre;
    String localidad;
    String provincia;

//getters and setters
    }

Could some one help me? Thank you in advance

EDIT: I let you my php code in case there is something wrong with it

if (mysql_num_rows($result) > 0) {
// looping through all results
$response["beers"] = array();

while ($row = mysql_fetch_array($result)) {

    $beers = array();
    $beers["id"] = $row["id"];
    $beers["nombre"] = $row["nombre"];
    $beers["localidad"] = $row["localidad"];
    $beers["provincia"] = $row["provincia"];

    // push single product into final response array
    array_push($response["beers"], $beers);
}
// success
$response["succes"] = 1;

// echoing JSON response
echo json_encode($response);
mysql_close($localhost);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No beers have found";

    // echo no users JSON
    echo json_encode($response);
    mysql_close($localhost);
}

EDIT 2: I solved the problem with this post https://stackoverflow.com/a/27698866/4669063

I created the LenientGsonConverter class I added in the setConverter method to my RestAdapter

Upvotes: 4

Views: 9597

Answers (2)

Bhavin Shah
Bhavin Shah

Reputation: 361

RestAdapter retrofit= new RestAdapter.Builder().setEndpoint("end_point_url")
                .build();
AbebeerApi service = retrofit.create(AbebeerApi.class);
service.getBeer(new Callback<BeerResponse>() {

            @Override
            public void success(BeerResponse beerResponse, Response res) {
                // Use beerResponse for response
            }

            @Override
            public void failure(RetrofitError error) {
            }
        });

Here is your interface. You should add Callback method. In callback you should pass your POJO.

public interface AbebeerApi {
    @GET("/beer_app/beers")
    public void getBeer(Callback<BeerResponse> callback)
}

I am answering for this api.

{"beers":[{"id":"1","nombre":"Larzobispa","localidad":"Alcorcon","provincia":"Madrid"},{"id":"2","nombre":"La Cibeles","localidad":"Madrid","provincia":"Madrid"}]}

The problem is you are trying to pass List directly. You should create two classes for getting response. The first one BeerResponse is only for list and in second class you should declare your json object means id ,nombre and all.Moreover the main import thing is your api fields name and your POJO's fields name must be same. If you want to change POJO's field name then you can use GSON annotation @SerializedName.

public class BeerResponse{
 List<SingleBeer> beers;

 //getters and setters
}

public class SingleBeer {
    int id;
    String nombre;
    String localidad;
    String provincia;

//getters and setters
}

Upvotes: 2

iagreen
iagreen

Reputation: 32016

The structure is not exactly the same. Your response has an object that has a field called beers that is a list of beers. You need to deserialize that object --

class Beers {
    List<SingleBeer> beers;
}

Update your interface --

public interface AbebeerApi {
    @GET("/beer_app/beers")
    Beers listBeers();
}

and your call --

Beers beerContainer = service.listBeers();

And access the beer list though beerContain.beers or an equivalent getter.

Upvotes: 1

Related Questions