如何使用"Retrofit"调用简单的GET方法

Ret*_*lai 17 android get retrofit

如果你帮我用Retrofit调用一个简单的API GET方法会很好.我已将Gson和Retrofit jar文件添加到构建路径中.

这是interface:

  public interface MyInterface {
        @GET("/my_api/shop_list")
        Response getMyThing(@Query("mid") String param1);
    }
Run Code Online (Sandbox Code Playgroud)

我只得到结果(在log cat中)如果我在AsyncTask中调用以下内容,我会得到NetworkOrMainThreadException如何调用它?

@Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("http://IP:Port/")
                .setLogLevel(RestAdapter.LogLevel.FULL).build();
        MyInterface service = restAdapter
                .create(MyInterface.class);
        mResponse = service.getMyThing("455744");

        return null;
    }
Run Code Online (Sandbox Code Playgroud)
  • 我是否真的必须在AsyncTask中调用Restadapter?
  • 如何从响应中获取JsonObject?

Mig*_*gne 30

Retrofit为您提供同步和异步选项.根据您声明接口方法的方式,它将是同步的或异步的.

public interface MyInterface {
    // Synchronous declaration
    @GET("/my_api/shop_list") 
    Response getMyThing1(@Query("mid") String param1);

    // Asynchronous declaration
    @GET("/my_api/shop_list")
    void getMyThing2(@Query("mid") String param1, Callback<Response> callback);
}
Run Code Online (Sandbox Code Playgroud)

如果您同步声明API,那么您将负责执行它Thread.

请阅读Retrofit 网站上的"SYNCHRONOUS VS. ASYNCHRONOUS VS. OBSERVABLE"部分.这将向您解释如何根据您的不同需求声明API的基础知识.

获取JSON类对象的最简单方法是将其映射到Java对象,然后让Retrofit为您进行转换.

例如,如果你的休息api返回的JSON是

[{"id":1, "name":"item1"}, {"id":2, "name":"item2"}]
Run Code Online (Sandbox Code Playgroud)

然后你可以创建一个Java类似的类

public class Item {
    public final int id;
    public final String name;

    public Item(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后简单地声明你的api就好了

@GET("/my_api/shop_list")
void getMyThing(@Query("mid") String param1, Callback<List<Item>> callback);  // Asynchronous
Run Code Online (Sandbox Code Playgroud)

并使用它

api.getMyThing("your_param_here", new Callback<List<Item>>() {
        @Override
        public void success(List<Item> shopList, Response response) {
            // accecss the items from you shop list here
        }

        @Override
        public void failure(RetrofitError error) {

        }
    });
Run Code Online (Sandbox Code Playgroud)

基于您在评论中提供的JSON,您可以对此进行此类操作

public class MyThingResponse {
    public InnerResponse response;
}

public class InnerResponse {
    public String message;
    public String status;
    public List<Item> shop_list;
}
Run Code Online (Sandbox Code Playgroud)

这有点难看,但这是因为JSON.我的建议是通过删除"响应"内部对象来简化您的JSON,如果可以的话,就像这样

{
    "message": "Shops shown",
    "status": 1,
    "shop_list": [
        {
            "id": "1",
            "city_id": "1",
            "city_name": "cbe",
            "store_name": "s"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

然后你的POJO可以变得更简单

public class MyThingResponse {
    public String message;
    public String status;
    public List<Item> shop_list;
}
Run Code Online (Sandbox Code Playgroud)