4

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

Community
  • 1
  • 1
Bruno
  • 299
  • 1
  • 7
  • 23
  • 1
    Looks like you need to add a convertor when building your adapter. See this answer http://stackoverflow.com/a/32390665/1904517 – iagreen Sep 27 '15 at 17:45
  • Thank you for your comment but still not work with that answer.... – Bruno Sep 27 '15 at 18:58
  • I just change my code and use retrofit 1.9.0 but still dont work. If I use https://api.github.com/users I can get all the users in my list but when I want to get my Json it doesnt work. It has the same structure...[{"id":"1","nombre":"La arzobispa","localidad":"Alcorcon","provincia":"Madrid"},{"id":"2","nombre":"La Cibeles","localidad":"Madrid","provincia":"Madrid"}] – Bruno Sep 27 '15 at 19:36

2 Answers2

2
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
}
Bhavin Shah
  • 361
  • 4
  • 11
  • Used and still not working. Thankyou for the info. I get in the error.printStackTrace() the following error com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $ – Bruno Sep 28 '15 at 16:11
1

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.

iagreen
  • 31,470
  • 8
  • 76
  • 90
  • Yes I changed my Json response to that: [{"id":"1","nombre":"La arzobispa","localidad":"Alcorcon","provincia":"Madrid"},{"id":"2","nombre":"La Cibeles","localidad":"Madrid","provincia":"Madrid"}] to obtain the same structure (with a string id) and still not working... I tried your code too but not working. Thank you for your time, – Bruno Sep 27 '15 at 20:08
  • 1
    See if you can get a logcat. That would help narrow things down – iagreen Sep 27 '15 at 20:10
  • In the service the methods succes and failure I know that the code runs through the failure obtaining my log in the logcat. @Override public void failure(RetrofitError error) { Log.i("TEST", "ERROR");}. I let you my url if you could see something wrong http://abdevelopers.site88.net/beer_app/beers/ – Bruno Sep 27 '15 at 20:20
  • 1
    Try putting an `error.printStackTrace()` in your on failure method to see why retrofit is unhappy with the request. – iagreen Sep 27 '15 at 20:35
  • retrofit.RetrofitError: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path This error is what i get from the error.printStackTrace() – Bruno Sep 28 '15 at 16:14
  • 1
    Are you still getting that error? Looking at your link above, there is an an analytics tag on the response as well that starts on line 2, that could be messing things up. – iagreen Oct 01 '15 at 02:08
  • 1
    Yes i solved my problem with that post http://stackoverflow.com/a/27698866/4669063. I created the LenientGsonConverter class I added in the setConverter method to my RestAdapter. And thank you very much to your help, you guide me in the right direction :) – Bruno Oct 02 '15 at 06:39