Android 如何使用";调用简单的GET方法;“改装”;

Android 如何使用";调用简单的GET方法;“改装”;,android,get,retrofit,Android,Get,Retrofit,如果您能帮助我使用改型调用一个简单的API GET方法,那就太好了。 我已经将Gson和改装jar文件添加到构建路径中 这是界面: public interface MyInterface { @GET("/my_api/shop_list") Response getMyThing(@Query("mid") String param1); } 如果我在AsyncTask中调用以下命令,我只会得到结果(在log cat中),否则我会得到Networ

如果您能帮助我使用改型调用一个简单的API GET方法,那就太好了。 我已经将Gson和改装jar文件添加到构建路径中

这是
界面

  public interface MyInterface {
        @GET("/my_api/shop_list")
        Response getMyThing(@Query("mid") String param1);
    }
如果我在AsyncTask中调用以下命令,我只会得到结果(在log cat中),否则我会得到
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;
    }
  • 我真的必须在AsyncTask中调用Restadapter吗
  • 如何从响应中获取JsonObject

改装为您提供了同步和异步选项。根据您声明接口方法的方式,它可以是同步的,也可以是异步的

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);
}
然后您可以像这样创建
Java

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

    public Item(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
然后简单地像这样声明您的api

@GET("/my_api/shop_list")
void getMyThing(@Query("mid") String param1, Callback<List<Item>> callback);  // Asynchronous
然后你的POJO就会变得像这样简单

public class MyThingResponse {
    public String message;
    public String status;
    public List<Item> shop_list;
}
公共类MyThingResponse{
公共字符串消息;
公共字符串状态;
公众名单店铺名单;
}

它很有用。我还有一个问题。我相信它是从json对象失败的。错误是<代码>失败,RefundationErrorRefundation.RefundationError:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_数组,但在第1行第2列为BEGIN_对象,但实际结果显示在日志中。我的回答是:
{“Response”:{“message”:“Shops show show”,“status”:1,“shop_list”:[{“id”:“1”,“city_id”:“1”,“city_name”:“cbe”,“store_name”:“s”,
我已经更新了答案。我的建议是通过删除“response”对象来简化服务器上的JSON响应。但是如果你不能,我已经解释了如何使用2个java类来映射它,但这有点难看。
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) {

        }
    });
public class MyThingResponse {
    public InnerResponse response;
}

public class InnerResponse {
    public String message;
    public String status;
    public List<Item> shop_list;
}
{
    "message": "Shops shown",
    "status": 1,
    "shop_list": [
        {
            "id": "1",
            "city_id": "1",
            "city_name": "cbe",
            "store_name": "s"
        }
    ]
}
public class MyThingResponse {
    public String message;
    public String status;
    public List<Item> shop_list;
}