首页 > 其他 > 详细

Retrofit框架的使用

时间:2016-05-19 14:45:01      阅读:163      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!