導航:首頁 > 編程語言 > retrofit20json

retrofit20json

發布時間:2024-12-16 09:38:29

1. 如何通過Retrofit提交json格式數據

本文將介紹如何通過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
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
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
FlyRouteBean flyRouteBean=new FlyRouteBean();
flyRouteBean=initdata(flyRouteBean);//根據Bean類初始化一個需要提交的數據類
Gson gson=new Gson();
String route= gson.toJson(flyRouteBean);//通過Gson將Bean轉化為Json字元串形式
[java] view plain
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'

2. refit默認windows啟動

refit默認windows啟動()1.簡介Refit是一個受到Square的Retrofit庫(Java)啟發的自動類型安全REST庫。通過HttpClient網路請求(POST,GET,PUT,DELETE等封裝)把REST API返回的數據轉化為POCO(Plain Ordinary C# Object,簡單C#對象) to JSON。我們的應用程序通過Refit請求網路,實際上是使用Refit介面層封裝請求參數、Header、Url等信息,之後由HttpClient完成後續的請求操作,在服務端返回數據之後,HttpClient將原始的結果交給Refit,後者根據用戶的需求對結果進行解析的過程。安裝組件命令行:
Install-Package refit代碼例子:
[Headers("User-Agent: Refit Integration Tests")]//這里因為目標源是GitHubApi,所以一定要加入這個靜態請求標頭信息,讓其這是一個測試請求,不然會返回數據異常。public interface IGitHubApi{ [Get("/users/{user}")] Task GetUser(string user);}public class GitHubApi{ public async Task GetUser() { var gitHubApi = RestService.For("https://api.github.com"); var octocat = await gitHubApi.GetUser("octocat"); return octocat; }}public class User{ public string login { get; set; } public int? id { get; set; } public string url { get; set; }}[HttpGet]public async Task<ActionResult<IEnumerable>> Get(){ var result = await new GitHubApi().GetUser(); return new string[] { result.id.Value.ToString(), result.login };}註:介面中Headers、Get這些屬性叫做Refit的特性。定義上面的一個IGitHubApi的REST API介面,該介面定義了一個函數GetUser,該函數會通過HTTP GET請求去訪問伺服器的/users/{user}路徑把返回的結果封裝為User POCO對象並返回。其中URL路徑中的{user}的值為GetUser函數中的參數user的取值,這里賦值為octocat。然後通過RestService類來生成一個IGitHubApi介面的實現並供HttpClient調用。

2.API屬性
每個方法必須具有提供請求URL和HTTP屬性。HTTP屬性有六個內置注釋:Get, Post, Put, Delete, Patch and Head,例:
[Get("/users/list")]您還可以在請求URL中指定查詢參數:
[Get("/users/list?sort=desc")]還可以使用相對URL上的替換塊和參數來動態請求資源。替換塊是由{and,即&}包圍的字母數字字元串。如果參數名稱與URL路徑中的名稱不匹配,請使用AliasAs屬性,例:
[Get("/group/{id}/users")]Task<List> GroupList([AliasAs("id")] int groupId);請求URL還可以將替換塊綁定到自定義對象,例:
[Get("/group/{request.groupId}/users/{request.userId}")]Task<List> GroupList(UserGroupRequest request);class UserGroupRequest{ int groupId { get;set; } int userId { get;set; }}未指定為URL替換的參數將自動用作查詢參數。這與Retrofit不同,在Retrofit中,必須明確指定所有參數,例:
[Get("/group/{id}/users")]Task<List> GroupList([AliasAs("id")] int groupId, [AliasAs("sort")] string sortOrder);GroupList(4, "desc");輸出結果:"/group/4/users?sort=desc"
3.動態查詢字元串參數(Dynamic Querystring Parameters)
方法還可以傳遞自定義對象,把對象屬性追加到查詢字元串參數當中,例如:
public class MyQueryParams{ [AliasAs("order")] public string SortOrder { get; set; } public int Limit { get; set; }}[Get("/group/{id}/users")]Task<List> GroupList([AliasAs("id")] int groupId, MyQueryParams params);[Get("/group/{id}/users")]Task<List> GroupListWithAttribute([AliasAs("id")] int groupId, [Query(".","search")]MyQueryParams params);params.SortOrder = "desc";params.Limit = 10;GroupList(4, params)輸出結果:"/group/4/users?order=desc&Limit=10"
GroupListWithAttribute(4, params)輸出結果:"/group/4/users?search.order=desc&search.Limit=10"您還可以使用[Query]指定querystring參數,並將其在非GET請求中扁平化,類似於:
[Post("/statuses/update.json")]Task PostTweet([Query]TweetParams params);5.集合作為查詢字元串參數(Collections as Querystring Parameters)方法除了支持傳遞自定義對象查詢,還支持集合查詢的,例:
[Get("/users/list")]Task Search([Query(CollectionFormat.Multi)]int[] ages);Search(new [] {10, 20, 30})輸出結果:"/users/list?ages=10&ages=20&ages=30"
[Get("/users/list")]Task Search([Query(CollectionFormat.Csv)]int[] ages);Search(new [] {10, 20, 30})輸出結果:"/users/list?ages=10%2C20%2C30"
6.轉義符查詢字元串參數(Unescape Querystring Parameters)
使用QueryUriFormat屬性指定查詢參數是否應轉義網址,例:
[Get("/query")][QueryUriFormat(UriFormat.Unescaped)]Task Query(string q);Query("Select+Id,Name+From+Account")輸出結果:"/query?q=Select+Id,Name+From+Account"
7.Body內容
通過使用Body屬性,可以把自定義對象參數追加到HTTP請求Body當中。
[Post("/users/new")]Task CreateUser([Body] User user)根據參數的類型,提供Body數據有四種可能性:●如果類型為Stream,則內容將通過StreamContent流形式傳輸。●如果類型為string,則字元串將直接用作內容,除非[Body(BodySerializationMethod.Json)]設置了字元串,否則將其作為StringContent。●如果參數具有屬性[Body(BodySerializationMethod.UrlEncoded)],則內容將被URL編碼。●對於所有其他類型,將使用RefitSettings中指定的內容序列化程序將對象序列化(默認為JSON)。●緩沖和Content-Length頭默認情況下,Refit重新調整流式傳輸正文內容而不緩沖它。例如,這意味著您可以從磁碟流式傳輸文件,而不會產生將整個文件載入到內存中的開銷。這樣做的缺點是沒有在請求上設置內容長度頭(Content-Length)。如果您的API需要您隨請求發送一個內容長度頭,您可以通過將[Body]屬性的緩沖參數設置為true來禁用此流行為:
Task CreateUser([Body(buffered: true)] User user);7.1.JSON內容使用Json.NET對JSON請求和響應進行序列化/反序列化。默認情況下,Refit將使用可以通過設置Newtonsoft.Json.JsonConvert.DefaultSettings進行配置的序列化器設置:
JsonConvert.DefaultSettings = () => new 電腦 JsonSerializerSettings() { ContractResolver = new (), Converters = {new StringEnumConverter()} };//Serialized as: {"day":"Saturday"}await PostSomeStuff(new { Day = DayOfWeek.Saturday });由於默認靜態配置是全局設置,它們將影響您的整個應用程序。有時候我們只想要對某些特定API進行設置,您可以選擇使用RefitSettings屬性,以允許您指定所需的序列化程序進行設置,這使您可以為單獨的API設置不同的序列化程序設置:
var gitHubApi = RestService.For("https://api.github.com", new RefitSettings { ContentSerializer = new JsonContentSerializer( new JsonSerializerSettings { ContractResolver = new () } )});var otherApi = RestService.For("https://api.example.com", new RefitSettings { ContentSerializer = new JsonContentSerializer( new JsonSerializerSettings { 電腦 ContractResolver = new () } )});還可以使用Json.NET的JsonProperty屬性來自定義屬性序列化/反序列化:
public class Foo{ //像[AliasAs(「 b」)]一樣會在表單中發布 [JsonProperty(PropertyName="b")] public string Bar { get; set; }} 7.2XML內容XML請求和響應使用System.XML.Serialization.XmlSerializer進行序列化/反序列化。默認情況下,Refit只會使用JSON將內容序列化,若要使用XML內容,請將ContentSerializer配置為使用XmlContentSerializer:
var gitHubApi = RestService.For("https://www.w3.org/XML", new RefitSettings { ContentSerializer = new XmlContentSerializer()});屬性序列化/反序列化可以使用System.Xml.serialization命名空間中的屬性進行自定義:
public class Foo{ [XmlElement(Namespace = "https://www.w3.org/XML")] public string Bar { get; set; }}System.Xml.Serialization.XmlSerializer提供了許多序列化選項,可以通過向XmlContentSerializer構造函數提供XmlContentSerializer設置來設置這些選項:
var 電腦 gitHubApi = RestService.For("https://www.w3.org/XML", new RefitSettings { ContentSerializer = new XmlContentSerializer( new XmlContentSerializerSettings { XmlReaderWriterSettings = new XmlReaderWriterSettings() { ReaderSettings = new XmlReaderSettings { IgnoreWhitespace = true } } } )});7.3.表單發布(Form posts)對於以表單形式發布(即序列化為application/x-www-form-urlencoded)的API,請使用初始化Body屬性BodySerializationMethod.UrlEncoded屬性,參數可以是IDictionary字典,例:
public interface IMeasurementProtocolApi{ [Post("/collect")] Task Collect([Body(BodySerializationMethod.UrlEncoded)] Dictionary data);}var data = new Dictionary { {"v", 1}, {"tid", "UA-1234-5"}, {"cid", new Guid("d1e9ea6b-2e8b-4699-93e0-0bcbd26c206c")}, {"t", "event"},};// Serialized as: v=1&tid=UA-1234-5&cid=d1e9ea6b-2e8b-4699-93e0-0bcbd26c206c&t=eventawait api.Collect(data);如果我們傳遞對象跟請求表單中欄位名稱不一致時,可在對象屬性名稱上加入[AliasAs("你定義欄位名稱")] 屬性,那麼加入屬性的對象欄位都將會被序列化為請求中的表單欄位:
public interface IMeasurementProtocolApi{ [Post("/collect")] Task Collect([Body(BodySerializationMethod.UrlEncoded)] Measurement measurement);}public class Measurement{ // Properties can be read-only and [AliasAs] isn't required public int v { get { return 1; } } [AliasAs("tid")] public string WebPropertyId { get; set; } [AliasAs("cid")] public Guid ClientId { get; set; } [AliasAs("t")] public string Type { get; set; } public object IgnoreMe { private get; set; }}var measurement = new Measurement { WebPropertyId = "UA-1234-5", ClientId = new Guid("d1e9ea6b-2e8b-4699-93e0-0bcbd26c206c"), Type = "event"};// Serialized as: v=1&tid=UA-1234-5&cid=d1e9ea6b-2e8b-4699-93e0-0bcbd26c206c&t=eventawait api.Collect(measurement);8.設置請求頭8.1靜態頭(Static headers)您可以為將headers屬性應用於方法的請求設置一個或多個靜態請求頭:
[Headers("User-Agent: Awesome Octocat App")][Get("/users/{user}")]Task GetUser(string user);通過將headers屬性應用於介面,還可以將靜態頭添加到API中的每個請求:
[Headers("User-Agent: Awesome Octocat App")]public interface IGitHubApi{ [Get("/users/{user}")] Task GetUser(string user); [Post("/users/new")] Task CreateUser([Body] User user);}8.2動態頭(Dynamic headers)如果需要在運行時設置頭的內容,則可以通過將頭屬性應用於參數來向請求添加具有動態值的頭:
[Get("/users/{user}")]Task GetUser(string user, [Header("Authorization")] string authorization);// Will add the header "Authorization: token OAUTH-TOKEN" to the requestvar user = await GetUser("octocat", "token OAUTH-TOKEN"); 8.3授權(動態頭rex)使用頭的最常見原因是為了授權。而現在大多數API使用一些oAuth風格的訪問令牌,這些訪問令牌會過期,刷新壽命更長的令牌。封裝這些類型的令牌使用的一種方法是,可以插入自定義的HttpClientHandler。這樣做有兩個類:一個是,它接受一個Func<Task>參數,在這個參數中可以生成簽名,而不必知道請求。另一個是,它接受一個Func<HttpRequestMessage,Task>參數,其中簽名需要有關請求的信息(參見前面關於Twitter的API的注釋),例如:
class : HttpClientHandler{ private readonly Func<Task> getToken; public (Func<Task> getToken) { if (getToken == null) throw new ArgumentNullException(nameof(getToken)); this.getToken = getToken; } protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // See if the request has an authorize header var auth = request.Headers.Authorization; if (auth != null) { var token = await getToken().ConfigureAwait(false); request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token); } return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); }}或者:
class : DelegatingHandler { readonly Func<HttpRequestMessage, Task> getToken; public (Func<HttpRequestMessage, Task> getToken, HttpMessageHandler innerHandler = null) : base(innerHandler ?? new HttpClientHandler()) { this.getToken = getToken ?? throw new ArgumentNullException(nameof(getToken)); } protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // See if the request has an authorize header var auth = request.Headers.Authorization; if (auth != null) { var token = await getToken(request).ConfigureAwait(false); request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token); } return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); } }雖然HttpClient包含一個幾乎相同的方法簽名,但使用方式不同。重新安裝未調用HttpClient.SendAsync。必須改為修改HttpClientHandler。此類的用法與此類似(示例使用ADAL庫來管理自動令牌刷新,但主體用於Xamarin.Auth或任何其他庫:
class LoginViewModel{ AuthenticationContext context = new AuthenticationContext(...); private async Task GetToken() { // The AcquireTokenAsync call will prompt with a UI if necessary // Or otherwise silently use a refresh token to return // a valid access token var token = await context.AcquireTokenAsync("http://my.service.uri/app", "clientId", new Uri("callback://complete")); return token; } public async Task LoginAndCallApi() { var api = RestService.For(new HttpClient(new (GetToken)) { BaseAddress = new Uri("https://the.end.point/") }); var location = await api.GetLocationOfRebelBase(); }}interface IMyRestService{ [Get("/getPublicInfo")] Task SomePublicMethod(); [Get("/secretStuff")] [Headers("Authorization: Bearer")] Task GetLocationOfRebelBase();}在上面的示例中,每當調用需要身份驗證的方法時,將嘗試獲取新的訪問令牌。由應用程序提供,檢查現有訪問令牌的過期時間,並在需要時獲取新的訪問令牌。
電腦

閱讀全文

與retrofit20json相關的資料

熱點內容
哪些地方網路信號更好些 瀏覽:753
jar反編輯工具 瀏覽:614
描述數據波動大小有哪些 瀏覽:584
u盤exfat可復制4g以上的文件嗎 瀏覽:667
a4大小的文件過塑多少錢 瀏覽:26
暢天游2app在哪裡下載 瀏覽:844
微信看文字的圖片 瀏覽:298
將文件直接粘入word 瀏覽:134
VIP解析APP有哪些 瀏覽:463
怎樣徹底卸載cad文件 瀏覽:829
iphone4港版 瀏覽:624
怎麼用命令打開程序錯誤 瀏覽:665
iphone6怎麼改控制中心 瀏覽:808
ns錯誤代碼 瀏覽:653
iphone4s如何取消軟體更新提示 瀏覽:538
jsp列印換行 瀏覽:662
哪個軟體可以編程手機軟體 瀏覽:554
如東如何學數控編程培訓 瀏覽:5
微信h5頁面怎麼修改 瀏覽:931
手機怎麼無法打開視頻文件夾 瀏覽:840

友情鏈接