retrofit2 添加拦截器

本文将介何通过retrofit库post一串json格式的数首先post的json数据格式如下:

{
"Id": "string",
"DeviceId": "string",
"Name": "string",
"SumDistance": 0,
"RouteNo": "string",
"SumPoints": 0,
"SetupTime": "2016-06-10T13:11:00.766Z",
"UsedTime": 0,
"Points": [
{
"Id": "string",
"RouteNo": "string",
"Name": "string",
"Longitude": "string",
"Latitude": "string",
"Height": 0,
"Distance": 0,
"Yaw": 0,
"Pitch": 0,
"Speed": 0,
"Usedtime": 0
}
]
}

通过安Android studio gsonformat插件,根据上面的json格式自成一个Bean类,本文命名为FlyRouteBean,

[java] view plain copy
package com.example.administrator.retrofitex;
import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

/**
* Created by Administrator on 2016/6/10.
*/
public class FlyRouteBean{

/**
* Id : string
* DeviceId : string
* Name : string
* SumDistance : 0
* RouteNo : string
* SumPoints : 0
* SetupTime : 2016-05-23T06:20:50.254Z
* UsedTime : 0
* Points : [{"Id":"string","RouteNo":"string","Name":"string","Longitude":"string","Latitude":"string","Height":0,"Distance":0,"Yaw":0,"Pitch":0,"Speed":0,"Usedtime":0}]
*/

public String Id;
public String DeviceId;
public String Name;
public double SumDistance;
public String RouteNo;
public int SumPoints;
public String SetupTime;
public double UsedTime;
/**
* Id : string
* RouteNo : string
* Name : string
* Longitude : string
* Latitude : string
* Height : 0
* Distance : 0
* Yaw : 0
* Pitch : 0
* Speed : 0
* Usedtime : 0
*/

public List<PointsBean> Points;

public String getId() {
return Id;
}

public void setId(String Id) {
this.Id = Id;
}

public String getDeviceId() {
return DeviceId;
}

public void setDeviceId(String DeviceId) {
this.DeviceId = DeviceId;
}

public String getName() {
return Name;
}

public void setName(String Name) {
this.Name = Name;
}

public double getSumDistance() {
return SumDistance;
}

public void setSumDistance(double SumDistance) {
this.SumDistance = SumDistance;
}

public String getRouteNo() {
return RouteNo;
}

public void setRouteNo(String RouteNo) {
this.RouteNo = RouteNo;
}

public int getSumPoints() {
return SumPoints;
}

public void setSumPoints(int SumPoints) {
this.SumPoints = SumPoints;
}

public String getSetupTime() {
return SetupTime;
}

public void setSetupTime(String SetupTime) {
this.SetupTime = SetupTime;
}

public double getUsedTime() {
return UsedTime;
}

public void setUsedTime(double UsedTime) {
this.UsedTime = UsedTime;
}

public List<PointsBean> getPoints() {
return Points;
}

public void setPoints(List<PointsBean> Points) {
this.Points = Points;
}

public static class PointsBean implements Parcelable {
public String Id;
public String RouteNo;
public String Name;
public String Longitude;
public String Latitude;
public double Height;
public double Distance;
public double Yaw;
public double Pitch;
public double Speed;
public double Usedtime;

public String getId() {
return Id;
}

public void setId(String Id) {
this.Id = Id;
}

public String getRouteNo() {
return RouteNo;
}

public void setRouteNo(String RouteNo) {
this.RouteNo = RouteNo;
}

public String getName() {
return Name;
}

public void setName(String Name) {
this.Name = Name;
}

public String getLongitude() {
return Longitude;
}

public void setLongitude(String Longitude) {
this.Longitude = Longitude;
}

public String getLatitude() {
return Latitude;
}

public void setLatitude(String Latitude) {
this.Latitude = Latitude;
}

public double getHeight() {
return Height;
}

public void setHeight(double Height) {
this.Height = Height;
}

public double getDistance() {
return Distance;
}

public void setDistance(double Distance) {
this.Distance = Distance;
}

public double getYaw() {
return Yaw;
}

public void setYaw(double Yaw) {
this.Yaw = Yaw;
}

public double getPitch() {
return Pitch;
}

public void setPitch(double Pitch) {
this.Pitch = Pitch;
}

public double getSpeed() {
return Speed;
}

public void setSpeed(double Speed) {
this.Speed = Speed;
}

public double getUsedtime() {
return Usedtime;
}

public void setUsedtime(double Usedtime) {
this.Usedtime = Usedtime;
}

@Override
public String toString() {
return "PointsBean{" +
"Id=" + Id + +
", RouteNo=" + RouteNo + +
", Name=" + Name + +
", Longitude=" + Longitude + +
", Latitude=" + Latitude + +
", Height=" + Height +
", Distance=" + Distance +
", Yaw=" + Yaw +
", Pitch=" + Pitch +
", Speed=" + Speed +
", Usedtime=" + Usedtime +
};
}

@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(Id);
dest.writeString(RouteNo);
dest.writeString(Name);
dest.writeString(Longitude);
dest.writeString(Latitude);
dest.writeDouble(Height);
dest.writeDouble(Distance);
dest.writeDouble(Yaw);
dest.writeDouble(Pitch);
dest.writeDouble(Speed);
dest.writeDouble(Usedtime);
}
public static final Creator<PointsBean> CREATOR=new Creator<PointsBean>() {

@Override
public PointsBean createFromParcel(Parcel source) {
// TODO Auto-generated method stub
PointsBean pointsBean=new PointsBean();
pointsBean.setId(source.readString());
pointsBean.setRouteNo(source.readString());
pointsBean.setName(source.readString());
pointsBean.setLongitude(source.readString());
pointsBean.setLatitude(source.readString());
pointsBean.setHeight(source.readInt());
pointsBean.setDistance(source.readInt());
pointsBean.setYaw(source.readInt());
pointsBean.setPitch(source.readInt());
pointsBean.setSpeed(source.readInt());
pointsBean.setUsedtime(source.readInt());
return pointsBean;
}

@Override
public PointsBean[] newArray(int size) {
// TODO Auto-generated method stub
return new PointsBean[size];
}

};

@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}

@Override
public String toString() {
return "FlyRouteBean{" +
"Id=" + Id + +
", DeviceId=" + DeviceId + +
", Name=" + Name + +
", SumDistance=" + SumDistance +
", RouteNo=" + RouteNo + +
", SumPoints=" + SumPoints +
", SetupTime=" + SetupTime + +
", UsedTime=" + UsedTime +
", Points=" + Points +
};
}

}
然后就来建立接口了,其内容如下:
[java] view plain copy
public interface PostRoute {
@Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头
@POST("api/FlyRoute/Add")
Call<FlyRouteBean> postFlyRoute(@Body RequestBody route);//传入的参数为RequestBody
}
接下来就是提交数据的了:
[java] view plain copy
FlyRouteBean flyRouteBean=new FlyRouteBean();
flyRouteBean=initdata(flyRouteBean);//根据Bean类初始化一个需要提交的数据类
Gson gson=new Gson();
String route= gson.toJson(flyRouteBean);//通过Gson将Bean转化为Json字符串形式
[java] view plain copy
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory( GsonConverterFactory.create())
.build();
PostRoute postRoute=retrofit.create(PostRoute.class);
RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route);
Call<FlyRouteBean> call=postRoute.postFlyRoute(body);
call.enqueue(new Callback<FlyRouteBean>() {
@Override
public void onResponse(Call<FlyRouteBean> call, Response<FlyRouteBean> response) {
Log.e("sssss","-----------------------"+response.body().getDeviceId());//这里是用于测试,服务器返回的数据就是提交的数据。
}

@Override
public void onFailure(Call<FlyRouteBean> call, Throwable t) {
Log.e("sssss",t.getMessage());
}
});
需要添加的依赖:
compile com.squareup.retrofit2:retrofit:2.0.2
compile com.squareup.retrofit2:converter-gson:2.0.0-beta3
喜欢阅读
  • 恶魔总裁宠上瘾

    恶魔总裁宠上瘾

    某女在电视前面趴着看时装秀,突然爬起来,不情愿地掀起小嘴:“我看以后还是给你做情人算了,好歹那个时候你还每月给钱呢。”某男不悦地从文件里面抬脸,危险地眯起眼,深邃的五官立体得如同雕塑一般:“你是在抱怨我没有给你钱花?”感觉到他身上的危险气息,她利索地缩了缩肩膀谄媚凑上去:“当然不是啦。。。只是要是你愿意依旧定时给钱就更好了,我一个堂堂的总裁夫人出去老是没有钱多不好......”“那好吧,我就给你这个机会,”翻身将她压下,“按次数?”还没有来得及说不,就被吃干抹净,哀怨地咬着小手绢,可怜巴巴地缩在角落里面,总裁什么的最讨厌了......

  • 错爱无期

    错爱无期

    她被纨绔逼婚,走投无路签下培训协议。培训期间她处处与他作对,他从没见过这种女人,被她惹得气怒至极、却又对她的个性又爱又恨,被深深吸引。一日,他将她抵在墙上,火花迸射、情难自禁之际——他捏着她的下巴,深深吻下去:“我后悔了!我要你成为我的女人。”

  • 器道无极

    器道无极

    剑锋所指、所向披靡。道剑弑神,器身破天!!看叶无极怎样在这片大陆上称霸的,怎样面对强大的敌人一步一步成长的!战士、战者、战师、大战师、战将、战王、战皇、战帝、战神,一级分为九星战战战战

  • 总裁霸爱:老婆哪里逃

    总裁霸爱:老婆哪里逃

    时隔五年再次相遇,宁冉带着儿子搅乱他的婚礼现场,随后再次逃之夭夭。他前后找了她六年,不顾一切将她绑回家里,“我可以死,但只会死在你的手里。”宁冉看着陌生的男人,凝眉不语,刻意无视蹦跶在一旁叫她妈妈的小屁孩儿。“那啥,我似乎不认识你们。”

  • 温情恋事

    温情恋事

    一夜旖旎,她给他五块,而他将十亿现金砸在她家门口。她被训练成妖娆百态的危机女公关,而他却是她的BOSS!“撩完我就想走?”男人盛怒将她身体禁锢在怀中。“这些撩人的手段可都是你教的。”她强力抗拒,明明是他要自己展示这些的。豪门暗夺,娱乐圈巨星,她屡战屡胜,护花使者能绕地球一圈,而她的BOSS原来是……

  • 八百年修仙记

    八百年修仙记

    封神大战后,众多神仙在人界就消失了踪迹。与此同时,大周王朝建立后又推崇人间道,抑制帮助自己夺取天下的道德之士,截教残余抵抗斗争更加激烈极端.。树欲静而风不止,一些诸侯积蓄实力野心勃勃开始扩张,大周封建制逐渐风雨飘摇,天下即将充满杀戮。

  • 借一曲风月情殇

    借一曲风月情殇

    结婚三年,我和梁戈双双出了轨,他跟初恋旧情复燃,我和上司酒后乱性,为了孩子,我不可能离婚,可这段摇摇欲坠的婚姻,我维持得很辛苦,“唐苏,跟他离婚。”“你娶我吗?”我知道,贺青帆不会娶我,他有未婚妻,我不过是他众多情、妇中的一个,可我却已经爱上了他……

  • 美人如玉:绝佳大小姐

    美人如玉:绝佳大小姐

    一次旅行,宿命的安排让她跨越千年重回到那刻骨铭心的过去。漫天梨花树下,俊美如谪仙的他从身后轻轻抱住她,他的声音带着一丝丝祈求。“如嫣,这辈子,下辈子,生生世世,你都不要离开我好不好?”他睨着她,俊美无暇的容颜上荡漾出一抹宠溺的笑容。“嫣儿,从来没有哪个女人能够让我如此痴狂,你做到了,还那样彻底,我爱你,所以,我必须把你牢牢的禁锢在我的身边。”她的眼底透着彻骨的寒意,如果没有你,思辰哥哥就是属于我的,为什么,你要出现,来破坏属于我的幸福,我恨你。当一次一次纠缠,最终牵出彼此的身世之谜以后,他们又该何去何从?他们的关系就像盘旋在大树上的蔓藤,越牵越紧,再也逃不开。

  • 猜你喜欢
  • 自定义
  • 短信
  • 内容有什么用
  • retrofit使用
  • retrofit
  • 的实现
  • axios
  • retrofit2
  • retrofit和协程
  • 热门推荐
  • 跟乐嘉学性格色彩
  • 倒计时flash
  • 面相看人
  • dnf卡牌对对翻
  • 罗马2全面战争秘籍
  • 车展时间
  • wifi伴侣破解版
  • 急性乳腺炎
  • 肩膀疼痛的治疗方法
  • 深海鱼油的作用
  • 冠心病怎么治疗
  • 颈椎病手麻
  • 月经痛经怎么办
  • 黑眼圈原因
  • tmp文件用什么打开
  • 有部分网页打不开
  • 煎蛋卷
  • 生化类小说
  • 女用避孕套
  • 简笔画表情
  • All Right Reserved 安福网