【问题标题】:Got stuck with List and retrofit2 and foreach被 List 和 retrofit2 和 foreach 卡住了
【发布时间】:2017-01-12 19:41:51
【问题描述】:

我正在尝试使用 Retrofit 2.1.0 从 1.9.0 升级 在响应中我无法运行我想在地图上放置的标记。 在 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 : pins) {

我收到一个错误

对于每个不适用于类型“retrofit2.call”

我确定我忽略了一些东西。我错过了什么?

【问题讨论】:

    标签: java list foreach retrofit2


    【解决方案1】:

    您需要使用您获得的retrofit2.Response 实例,并从中获得body of the response,即List&lt;MapPins&gt;

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

    但你也应该检查response was successful:

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

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多