改造项目列表始终为空

Retrofit list of items always null

我试图从我的 android 应用程序的 WS 中获取项目列表,但响应始终为空。

这是我的代码,我看不懂:

接口:

public interface CategoriesInterface {

   @GET("/categories")
   List<CategorieModel> getCategories(@Query("k") String token);
}

异步任务:

    new AsyncTask<String, Void, Void>() {
    @Override
    protected Void doInBackground(String... params) {
        try {
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint( "WS_URL" ) // The base API endpoint.
                    .setConverter( new GsonConverter(gson) )
                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .build();

            CategoriesInterface categoriesInterface = restAdapter.create( CategoriesInterface.class );

            List<CategorieModel> list = categoriesInterface.getCategories( params[0] );

            // LIST IS ALWAYS NULL

        } catch (RetrofitError error) {
            Log.e("ERROR", error.toString());
        }

        return null;
    }

}.execute( token );

型号:

public class CategorieModel {

   @SerializedName("id")
   private String id;

   @SerializedName("cat_pt")
   private String cat_pt;

   @SerializedName("cat_es")
   private String cat_es;
}

响应JSON格式:

 "categories": [
    {
        "id": "2",
        "cat_pt": “STRING",
        "cat_es": “STRING"
    },

我不知道我做错了什么。响应列表始终为空。

谢谢

你的JSONreturns一个CategoryContainer,里面包含一个CategorieModel数组。添加新模型 class:

public class CategorieContainer {
    private List<CategorieModel> categories = new ArrayList<>();
    // TODO Getter / Setter
}

并更改您的 Retrofit-Interface:

@GET("/categories")
CategorieContainer getCategories(@Query("k") String token);