如何使用retrofit发送网络请求
1个回答
展开全部
用Retrofit发送网络请求和解析json的实例
Retrofit是Android的一个非常好用的开源HTTP Request。现在介绍一下Retrofit是如何使用的。。。。
首先是导入Retrofit包,
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
}1234512345
然后是根据API的JSON数据建立一个数据类
URL:http://apistore.baidu.com/microservice/weather?citypinyin=beijing
{
errNum: 0,
errMsg: "success",
retData: {
city: "北京", //城市
pinyin: "beijing", //城市拼音
citycode: "101010100", //城市编码
date: "15-02-11", //日期
time: "11:00", //发布时间
postCode: "100000", //邮编
longitude: 116.391, //经度
latitude: 39.904, //维度
altitude: "33", //海拔
weather: "晴", //天气情况
temp: "10", //气温
l_tmp: "-4", //最低气温
h_tmp: "10", //最高气温
WD: "无持续风向", //风向
WS: "微风(<10m/h)", //风力
sunrise: "07:12", //日出时间
sunset: "17:44" //日落时间
}
}
123456789101112131415161718192021222324123456789101112131415161718192021222324
以下是数据类:
public class Result {
private String errNum;
private String errMsg;
private WeatherData retData;
public void setErrNum(String errNum) {
this.errNum = errNum;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public WeatherData getRetData() {
return retData;
}
public void setRetData(WeatherData retData) {
this.retData = retData;
}
public String getErrNum() {
return errNum;
}
public String getErrMsg() {
return errMsg;
}
}
public class WeatherData {
private String city; //城市
private String pinyin;//城市拼音
private String citycode; //城市编码
private String date; //日期
private String time;//发布时间
private String postCode; //邮编
private String longitude;//经度
private String latitude; //维度
private String altitude;//海拔
private String weather; //天气情况
private String temp; //气温
private String l_tmp; //最低气温
private String h_tmp; //最高气温
private String WD; //风向
private String WS; //风力
private String sunrise;//日出时间
private String sunset;//日落时间
//setter和getter就不贴了
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
新建一个MyService的接口,由于之后要在主线程使用(安卓3.0以上主线程不能同步访问网络),所以这里采用异步获取的方式。故增加了Callback< Result > cb
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Query;
public interface MyService {
// URI:http://apistore.baidu.com/microservice/weather?citypinyin=beijing
@GET("/microservice/weather")
void getResult(@Query("citypinyin") String citypinyin, Callback<Result> cb);
}12345678910111234567891011
使用RestAdapter来实例化MyService;
import retrofit.RestAdapter;
public class MyRestClient {
private static String API_URL = "http://apistore.baidu.com";
public static MyService getService() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)//设置站点路径
.setLogLevel(RestAdapter.LogLevel.FULL)//设置log的级别
.build();
MyService myService = restAdapter.create(MyService.class);
return myService;
}
}123456789101112131415161718123456789101112131415161718
系统调用如下
@Override
public void onStart()
{
super.onStart();
MyRestClient.getService().getResult("beijing",new Callback<Result>() {
@Override
public void success(Result result, Response response) {
Log.i("",result.getRetData().getDate());
}
@Override
public void failure(RetrofitError error) {
}
});
}12345678910111213141516171234567891011121314151617
别忘了添加网络访问权限哟。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Retrofit是Android的一个非常好用的开源HTTP Request。现在介绍一下Retrofit是如何使用的。。。。
首先是导入Retrofit包,
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
}1234512345
然后是根据API的JSON数据建立一个数据类
URL:http://apistore.baidu.com/microservice/weather?citypinyin=beijing
{
errNum: 0,
errMsg: "success",
retData: {
city: "北京", //城市
pinyin: "beijing", //城市拼音
citycode: "101010100", //城市编码
date: "15-02-11", //日期
time: "11:00", //发布时间
postCode: "100000", //邮编
longitude: 116.391, //经度
latitude: 39.904, //维度
altitude: "33", //海拔
weather: "晴", //天气情况
temp: "10", //气温
l_tmp: "-4", //最低气温
h_tmp: "10", //最高气温
WD: "无持续风向", //风向
WS: "微风(<10m/h)", //风力
sunrise: "07:12", //日出时间
sunset: "17:44" //日落时间
}
}
123456789101112131415161718192021222324123456789101112131415161718192021222324
以下是数据类:
public class Result {
private String errNum;
private String errMsg;
private WeatherData retData;
public void setErrNum(String errNum) {
this.errNum = errNum;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public WeatherData getRetData() {
return retData;
}
public void setRetData(WeatherData retData) {
this.retData = retData;
}
public String getErrNum() {
return errNum;
}
public String getErrMsg() {
return errMsg;
}
}
public class WeatherData {
private String city; //城市
private String pinyin;//城市拼音
private String citycode; //城市编码
private String date; //日期
private String time;//发布时间
private String postCode; //邮编
private String longitude;//经度
private String latitude; //维度
private String altitude;//海拔
private String weather; //天气情况
private String temp; //气温
private String l_tmp; //最低气温
private String h_tmp; //最高气温
private String WD; //风向
private String WS; //风力
private String sunrise;//日出时间
private String sunset;//日落时间
//setter和getter就不贴了
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
新建一个MyService的接口,由于之后要在主线程使用(安卓3.0以上主线程不能同步访问网络),所以这里采用异步获取的方式。故增加了Callback< Result > cb
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Query;
public interface MyService {
// URI:http://apistore.baidu.com/microservice/weather?citypinyin=beijing
@GET("/microservice/weather")
void getResult(@Query("citypinyin") String citypinyin, Callback<Result> cb);
}12345678910111234567891011
使用RestAdapter来实例化MyService;
import retrofit.RestAdapter;
public class MyRestClient {
private static String API_URL = "http://apistore.baidu.com";
public static MyService getService() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)//设置站点路径
.setLogLevel(RestAdapter.LogLevel.FULL)//设置log的级别
.build();
MyService myService = restAdapter.create(MyService.class);
return myService;
}
}123456789101112131415161718123456789101112131415161718
系统调用如下
@Override
public void onStart()
{
super.onStart();
MyRestClient.getService().getResult("beijing",new Callback<Result>() {
@Override
public void success(Result result, Response response) {
Log.i("",result.getRetData().getDate());
}
@Override
public void failure(RetrofitError error) {
}
});
}12345678910111213141516171234567891011121314151617
别忘了添加网络访问权限哟。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询