如何使用gson/retrofit获取对象数组? [英] How to get array of objects with gson/retrofit?

查看:537
本文介绍了如何使用gson/retrofit获取对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前曾使用gson自动转换为pojo.

I have used gson before to automatically convert to pojo's.

但是现在我正在尝试使用改造功能将api结果转换为对象.

but now im trying to use retrofit to convert an api result to objects.

只要json命名了对象数组,这是没有问题的:

As long as the json has named arrays of objects this is no problem:

{
    items:[
        {"name":"foo"},
        {"name":"bar"}
    ]
}


public class AnItem {
    String name;
}

public class MyItems {
    List<AnItem> items;
}

public interface MyApi {
    @GET("/itemlist")
    Call<MyItems> loadItems();
}

public boolean onOptionsItemSelected(MenuItem item) {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://someurl.com/api")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MyApi myApi = retrofit.create(MyApi.class);
    Call<MyApi> call = myApi.loadItems();
    call.enqueue(this);
    return true;
}

@Override
public void onResponse(Response<MyItems> response, Retrofit retrofit) {
    ArrayAdapter<AnItem> adapter = (ArrayAdapter<AnItem>) getListAdapter();
    adapter.clear();
    adapter.addAll(response.body().items);
}

这将自动工作.

但是如果未命名json数组怎么办?

but how to do this if the json array isn't named?

[
    {"name":"foo"},
    {"name":"bar"}
]

当不使用parsejson进行翻新并遍历数组以获取对象时,我知道该怎么做,但是我不能那样做,因为那部分是通过翻新和gson自动化的.

i know how to do it when not using retrofit with parsejson and iterate over array to get my objects but i cant do it like that because that part is automated with retrofit and gson.

我真的在这个问题上停留了一段时间了,任何指针都会受到感激.

ive been really stuck on this problem for some time now and any pointers would be really appreciated..

更新:

below are the parts i have changed MyItems to List<AnItem>:



public class AllTemplatesRetroActivity extends ListActivity implements Callback<List<AnItem>> {

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    ArrayAdapter<AnItem> arrayAdapter =
            new ArrayAdapter<AnItem>(this,
                    android.R.layout.simple_list_item_1,
                    android.R.id.text1,
                    new ArrayList<AnItem>());
    setListAdapter(arrayAdapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://someurl.com/api")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

    MyApi myApi = retrofit.create(MyApi.class);

    Call<List<AnItem>> call = myApi.loadItems();
    call.enqueue(this);
    return true;
}

@Override
public void onResponse(Response<List<AnItem>> response, Retrofit retrofit) {
    ArrayAdapter<AnItem> adapter = (ArrayAdapter<AnItem>) getListAdapter();
    adapter.clear();
    Log.i("##MYLOG###", "## response:"+ response.message());
    adapter.addAll(response.body());
}

推荐答案

像这样处理json响应:

To Handle json response like this:

[
    {"name":"foo"},
    {"name":"bar"}
]

使用通话<清单< AnItem >>

Use Call< List < AnItem>>

在Retrofit2中,HttpUrl.resolve()用于解析baseUrl和带注释的路径.

In Retrofit2, HttpUrl.resolve() is used to resolve baseUrl and annotated path.

由于其工作原理,

这有效:

.baseUrl("http://someurl.com")

@GET("/api/itemlist")    --> resolved to http://someurl.com/api/itemlist

或者这有效

.baseUrl("http://someurl.com/api/")

@GET("itemlist")    --->  resolved to http://someurl.com/api/itemlist

但是,这不起作用

.baseUrl("http://someurl.com/api")

 @GET("/itemlist")   ---> resolved to http://someurl.com/itemlist

这篇关于如何使用gson/retrofit获取对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆