Android – get JSON array using retrofit

Hi, I'm trying to parse the following JSON responses in the list

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

I use the following code

My interface

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

Implementation of abebeerapi

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

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

And call for the creation of services

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

I didn't get any response in the beer list. My application closed and I didn't get any information in logcat

This is my POJO course

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

//getters and setters
    }

Can anyone help me? Thank you first

Editor: I'll tell you my PHP code in case there's anything 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);
}

Editor 2: I solved this problem with this article https://stackoverflow.com/a/27698866/4669063

I created the lenientgsonconverter class that I added to my restadapter in the setconverter method

resolvent:

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) {
            }
        });

This is your interface. You should add a callback method. In the callback, you should use your POJO

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

I'm answering this API

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

The problem is that you try to pass the list directly. You should create two classes to get the response. The first beerresponse is only used for the list. In the second class, you should declare your JSON object, which means ID, nonmbre and all. Moreover. The main import is your API field name, and the field name of your POJO must be the same. If you want to change the field name of the POJO, 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
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>