码迷,mamicode.com
首页 > 其他好文 > 详细

Retrofit框架的使用

时间:2016-05-19 14:45:44      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

一、Get请求

 

private void Retrofit_Get() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Urls.baseUrl)
.build();
MyServerInterface myServerInterface = retrofit.create(MyServerInterface.class);
Call<ResponseBody> call = myServerInterface.getLastJsonString();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
String json = null;
try {
json = response.body().string();
mTextView.setText(json);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d(TAG, "onFailure() called with: " + "call = [" + call + "], t = [" + t + "]");
}
});
}

服务接口可以这样写:
public interface MyServerInterface {
@GET("341-2?maxResult=20&page=&showapi_appid=19170&showapi_sign=248b52a91c9d4d2fa5ca1ddd16ee7832")
Call<ResponseBody> getLastJsonString();

@GET("341-2?maxResult=20&page=&showapi_appid=19170&showapi_sign=248b52a91c9d4d2fa5ca1ddd16ee7832")
Call<JokeModel> getJson2Model();
}


















Retrofit框架的使用

标签:

原文地址:http://www.cnblogs.com/GeChuangGuang/p/5508482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!