发布网友 发布时间:2022-04-24 14:23
共1个回答
热心网友 时间:2023-10-16 06:03
用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" //日落时间
}
}
12345671011121314151617181920212223241234567101112131415161718192021222324
以下是数据类:
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就不贴了
}
1234567101112131415161718192021222324252627282930313233343536373839404142434445474849505152535455565712345671011121314151617181920212223242526272829303132333435363738394041424344454748495051525354555657
新建一个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);
}1234567101112345671011
使用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;
}
}12345671011121314151617181234567101112131415161718
系统调用如下
@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) {
}
});
}1234567101112131415161712345671011121314151617
别忘了添加网络访问权限哟。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>