Retrofit框架的使用

一、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();
}


















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