Retrofit超単純demo
5385 ワード
1.インタフェースを定義
ここではgetを使うかpostを使うかを明記しています
入力パラメータ
ここでQueryはurlのパラメータとして使用され、「user/john?password=xxxx"で、Pathはパスのエントリを置き換えるために使用されます.「user/{username}」のように2.beanオブジェクトを定義します.Gson
public interface ShopService {
@GET("/course_api/wares/hot")
Call getShop(@Query("pageSize") int pageSize,
@Query("curPage") int curPage);
}
ここではgetを使うかpostを使うかを明記しています
@Query("curPage")
入力パラメータ
ここでQueryはurlのパラメータとして使用され、「user/john?password=xxxx"で、Pathはパスのエントリを置き換えるために使用されます.「user/{username}」のように2.beanオブジェクトを定義します.Gson
/**
* error_code : 0
* reason : Success
* result : {"data":[{"content":" , , , , , 【 , 】 , , , 。 , 【 】 。","hashId":"9a05858007372df8ab297f104c047420","unixtime":1488518030,"updatetime":"2017-03-03 13:13:50"}]}
*/
private int error_code;
private String reason;
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
3.activityでRetrofit retrofit = new Retrofit.Builder()
.baseUrl("http://112.124.22.238:8081/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ShopService service = retrofit.create(ShopService.class);
//pageSize = 10,curPage=1
Call callShops = service.getShop(10,1);
callShops.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Log.e("response",response.toString());
}
@Override
public void onFailure(Call call, Throwable t) {
Log.e("failure","fail");
}
});
urlが合計3箇所表示され、base+Get+入力パラメータ