您好,登錄后才能下訂單哦!
本文將介紹如何通過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,
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 + '}'; } }
然后就來建立接口了,其內容如下:
public interface PostRoute { @Headers({"Content-Type: application/json","Accept: application/json"})//需要添加頭 @POST("api/FlyRoute/Add") Call<FlyRouteBean> postFlyRoute(@Body RequestBody route);//傳入的參數為RequestBody }
接下來就是提交數據的了:
FlyRouteBean flyRouteBean=new FlyRouteBean(); flyRouteBean=initdata(flyRouteBean);//根據Bean類初始化一個需要提交的數據類 Gson gson=new Gson(); String route= gson.toJson(flyRouteBean);//通過Gson將Bean轉化為Json字符串形式
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()); } });
<----------- 更新如何同時提交json數據和其他查詢字段:--------------->
服務器端(ASP):核心代碼如下,接收一個json格式學生類,加一個bool類型
[HttpPost] public IHttpActionResult Info(Student stu, bool IsGay) { return Ok(stu.Name); }
客戶端:
//接口地址:http://172.20.69.209:9665/api/Test/Info?IsGay={IsGay} @POST("api/Test/Info") Call<ResponseBody> postStudent(@Body RequestBody stu,@Query("IsGay") boolean IsGay);
實現代碼:
Student stu=new Student(); stu.setId("20103177"); stu.setName("zpm"); stu.setAge(18); Gson gson=new Gson(); String route= gson.toJson(stu); Log.e("post", "http://"); Retrofit retrofit=new Retrofit.Builder() .baseUrl("http://172.20.69.209:9665/") .addConverterFactory( GsonConverterFactory.create()) .build(); PostRoute postRoute=retrofit.create(PostRoute.class); RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route); Call<ResponseBody> call=postRoute.postStudent(body,true); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { Log.i("onResponse", response.body().string()); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Log.e("onFailure", t.getMessage()); } }); Log.e("postjson", route);
結果:
具體代碼已經傳到github
PS:本文中服務器都是樓主在局域網搭建的,
需要添加的依賴:
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
源代碼下載
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。