我正在尝试使用从 1.9.0 升级的 Retrofit 2.1.0 在响应中我无法运行我想要放在 map 上的标记。 在 Retrofit 1.9.0 上它工作得很好,但想要使用更新的系统。
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 : pin) {
我收到错误
for each not applicable to type 'retrofit2.call'
我确信我忽略了一些东西。我错过了什么?
最佳答案
您需要使用retrofit2.Response
您获得的实例,从中您可以获得 body of the response这是 List<MapPins>
:
for (MapPins pin : response.body()) {
// ...
}
但您还应该检查 response was successful :
if (!response.isSuccessful()) {
// handle
}
关于java - 被 List、retrofit2 和 foreach 困住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39330530/