小编Łuk*_*ski的帖子

如何从Retrofit onResponse返回方法中的数据?

我是改装的新手,我想让我的getData方法返回一个功能对象.最简单的方法是什么?

DataService.java

public class DataService {

    private static final String TAG = MainActivity.class.getSimpleName();
    private final ApiClient apiClient;

    public DataService() {
        apiClient = new ApiClientFactory().createApiClient();
    }

    public List<Feature> getData(){

        apiClient.getData().enqueue(new Callback<DataResponse>() {

            @Override
            public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
                List<Feature> features = response.body().getFeatures();
                Log.d(TAG, "Data successfully downloaded");
            }

            @Override
            public void onFailure(Call<DataResponse> call, Throwable t) {
                Log.e(TAG, t.toString());
            }
        });
        //I need to return features in getData method
    }
}
Run Code Online (Sandbox Code Playgroud)

java android retrofit2

6
推荐指数
1
解决办法
7155
查看次数

标签 统计

android ×1

java ×1

retrofit2 ×1