如何使用 retrofit 2 在列表视图中显示 json 数据

How to display json data in listview with retrofit 2

我想做的:用我将从 api.

中以 json 形式检索的电台名称填充列表视图

问题:我在使用站点名称填充列表视图时遇到问题,不确定要采取什么步骤。当我按下按钮时,什么也没有发生。任何朝着正确方向的努力将不胜感激。下面是我到目前为止的代码。

我的站台模型

public class Station {
    @SerializedName("id")
    int mStationId;
    @SerializedName("name")
    String mStationName;
    @SerializedName("lat")
    double mStationLat;
    @SerializedName("long")
    double mStationLong;

public Station(int mStationId, String mStationName, double mStationLat, double mStationLong) {
    this.mStationId = mStationId;
    this.mStationName = mStationName;
    this.mStationLat = mStationLat;
    this.mStationLong = mStationLong;
}
}

我的服务生成器

public class ServiceGenerator {

public static final String API_BASE_URL = "http://keantrolley.bbapi.co/stations/";

private static OkHttpClient httpClient = new OkHttpClient();
private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

public static  <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.client(httpClient).build();
    return retrofit.create(serviceClass);
}
} 

然后是我的界面

public interface StationClient {
@GET("station")
Call<List<Station>> listStations();
}

我的主要活动

public class MainActivity extends AppCompatActivity {

private ListView stationsListView;
private ArrayAdapter<Station> stationListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    stationsListView = (ListView) findViewById(android.R.id.list);

    stationListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new LinkedList<Station>());
    stationsListView.setAdapter(stationListAdapter);
}

public void getStations(View view) {
    stationListAdapter.clear();
    stationListAdapter.notifyDataSetChanged();

    StationClient stationClient = ServiceGenerator.createService(StationClient.class);
    Call<List<Station>> call = stationClient.listStations();
    call.enqueue(new Callback<List<Station>>() {
        @Override
        public void onResponse(Response<List<Station>> response, Retrofit retrofit) {
            if (response.isSuccess()) {
                stationListAdapter.addAll(); //Here is where i believe is the root of the problem
            } else {
                stationListAdapter.addAll();
            }
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

}

要获取列表,您必须致电 response.body():

StationClient stationClient = ServiceGenerator.createService(StationClient.class);
    Call<List<Station>> call = stationClient.listStations();
    call.enqueue(new Callback<List<Station>>() {
        @Override
        public void onResponse(Response<List<Station>> response, Retrofit retrofit) {
            if (response.isSuccess()) {
                stationListAdapter.addAll(response.body()); //Here is where i believe is the root of the problem
            } else {
                //Here is when the status code isn't 2XX (200,201,etc)
            }
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });

更新

为了在您的列表视图中正确显示电台名称:

ArrayAdapter<Station> adapter = new ArrayAdapter<Station>(this,
           android.R.layout.simple_list_item_1                
            , new LinkedList<Station>()) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {   

            ((TextView) convertView).setText(getItem(position).mStationName);
            return convertView;
        }            
    };