2016-09-05 120 views
0

我想使用Retrofit 2.1.0從1.9.0升級在響應上我無法運行我想放在地圖上的標記。 在Retrofit 1.9.0上它很完美,但想要使用更新的系統。卡住List和retrofit2和foreach

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

在行:(MapPins pin : pins) {

我得到一個錯誤

每個不適用型 'retrofit2.call'

我相信我忽視的東西。我錯過了什麼?

回答

2

您需要使用retrofit2.Response實例,你得到的,並從中你可以得到body of the response這是一個List<MapPins>

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

但你也應該檢查response was successful

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

非常感謝它現在的作品。 – Charley57