Charley57
Charley57

Reputation: 83

Got stuck with List and retrofit2 and foreach

I am trying to use Retrofit 2.1.0 Upgrading from 1.9.0 On the response I am not able to run the markers I want to put on a map. On Retrofit 1.9.0 it worked perfect but want to use an updated system.

Retrofit adapter = new Retrofit.Builder()
                .baseUrl( AppConfig.URL_PINS )
                .addConverterFactory(GsonConverterFactory.create())
                .build();

MapPinsApiInterface pinsApiInterface = adapter.create(MapPinsApiInterface.class);
Call<List<MapPins>> pins = (Call<List<MapPins>>) pinsApiInterface.getStreams();
pins.enqueue(new Callback<List<MapPins>>() {
    @Override
    public void onResponse(Call<List<MapPins>> pins, Response<List<MapPins>> response) {
        if (!isAdded() || pins == null )
            return;

        int numMarkersInRainbow[] = {
            R.mipmap.homecenter,
            R.mipmap.shop,
            R.mipmap.marksilv,
            R.mipmap.markgold,
            R.mipmap.markgreen,
            R.mipmap.markoran,
            R.mipmap.itsyou,
            R.mipmap.no_gps
        };
        bounds = new LatLngBounds.Builder();

        for (MapPins pin : pins) {    <***** Here is my error ***>
            MarkerOptions options = new MarkerOptions().position(new LatLng(pin.getLatitude(), pin.getLongitude()));
            options.title(pin.getName());
            options.snippet(pin.getDepth());
            options.icon(BitmapDescriptorFactory.fromResource(numMarkersInRainbow[pin.getMarker()]));

            Marker mkr = googleMap.addMarker(options);
            mMarkers.put(mkr.getId(), pin.getSiteid());
            bounds.include(new LatLng(pin.getLatitude(), pin.getLongitude()));
        }
    }

In the line: (MapPins pin : pins) {

I get an error

for each not applicable to type 'retrofit2.call'

I am sure I am overlooking something. What am I missing?

Upvotes: 0

Views: 690

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164129

You need to use the retrofit2.Response instance that you get, and from it you can get the body of the response which is a List<MapPins>:

for (MapPins pin : response.body()) {
    // ...
}

But you should also check that the response was successful:

if (!response.isSuccessful()) {
    // handle
}

Upvotes: 3

Related Questions