Retrofit 2 Spinner无法获取数据

时间:2018-04-08 07:12:50

标签: android spinner retrofit2

我想使用retrofit 2制作一个微调器,这是我的微调器类:

private void initSpinnerMeridian(){
        loading = ProgressDialog.show(mContext, null, "Loading...", true, false);

    mApiInterface.getMeridian().enqueue(new Callback<DataModel>() {
        @Override
        public void onResponse(Call<DataModel> call, Response<DataModel> response) {
            if (response.isSuccessful()) {
                loading.dismiss();
                List<Meridian> dataMeridian = response.body().getMeridian();
                List<String> listSpinner = new ArrayList<String>();
                for (int i = 0; i < dataMeridian.size(); i++){
                    listSpinner.add(dataMeridian.get(i).getNama());
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
                        android.R.layout.simple_spinner_item, listSpinner);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spMeridian.setAdapter(adapter);
            } else {
                loading.dismiss();
                Toast.makeText(mContext, "Failed get data from api", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<DataModel > call, Throwable t) {
            t.printStackTrace();
            loading.dismiss();
            Toast.makeText(mContext, "Check your internet connection", Toast.LENGTH_SHORT).show();
        }
    });
}

这是我的ApiInterface课程:

public interface ApiInterface {
@GET("/api/data/meridian")
Call<DataModel> getMeridian();

@GET("/data/proyek")
Call<DataModel> getProyek();

@FormUrlEncoded
@POST("/transaksi")
Call<InputModel> postTransaksi(
        @Field("save") String save,
        @Field("id_meridian") String idMeridian,
        @Field("id_proyek") String idProyek,
        @Field("kegiatan") String kegiatan,
        @Field("nominal") String nominal,
        @Field("id_kategori") String idKategori,
        @Field("tanggal") String tanggal,
        @Field("keterangan") String keterangan,
        @Field("status") Object status,
        @Field("images") ImageView images
);

}

但是,当我在模拟器上运行它时,没有任何反应,只有一个带有文本的吐司:Failed get data from api确实出现了。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您需要解析API中的非成功错误响应。 api可能返回原因(即未授权,路由不存在等等)。

我建议您从这里阅读并实施一些内容https://futurestud.io/tutorials/retrofit-2-simple-error-handling

这篇文章也许有帮助:https://stackoverflow.com/a/38243723/6407116

请尝试以下内容并查看日志

...
} else {
    loading.dismiss();
    Toast.makeText(mContext, "Failed get data from api", Toast.LENGTH_SHORT).show();
    Log.d("Error " + response.errorBody().string());
}

额外说明:

我不确定你的okhttpclient是如何构建的,但你可以像下面一样添加一个HttpLoggingInterceptor,以便更好地理解你的请求所发生的一切。

OkHttpClient providesOkHttpClient() {
    OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
            .readTimeout(30, TimeUnit.SECONDS)
            .connectTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS);

    if (BuildConfig.DEBUG) {
        // Add a logging interceptor for debug builds only
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
    }
    return builder.build();
}