Api Xamarin Forms Refit响应为False
我正在尝试使用Refit将数据作为x-www-form-urlencoded发送到api。但由于某些原因,无法检索响应。我一步一步地学习了很多教程,但都没有效果。有什么问题吗?提前谢谢 请求:Api Xamarin Forms Refit响应为False,api,xamarin,xamarin.forms,refit,x-www-form-urlencoded,Api,Xamarin,Xamarin.forms,Refit,X Www Form Urlencoded,我正在尝试使用Refit将数据作为x-www-form-urlencoded发送到api。但由于某些原因,无法检索响应。我一步一步地学习了很多教程,但都没有效果。有什么问题吗?提前谢谢 请求: public class SynchronizerApiRequest { public SynchronizerApiRequest() { } [JsonProperty("username")] public string
public class SynchronizerApiRequest
{
public SynchronizerApiRequest() { }
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
[JsonProperty("last_sync_date")]
public string LastSyncDate { get; set; }
}
答复:
public class SynchronizerApiResponse
{
public SynchronizerApiResponse() { }
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("tables")]
public SyncData Tables { get; set; }
}
同步数据:
public class SyncData
{
[JsonProperty("hometab")]
public List<HomeTab> HomeTabs { get; set; }
[JsonProperty("category")]
public List<Category> Categories { get; set; }
[JsonProperty("doaa")]
public List<Doaa> Doaas { get; set; }
[JsonProperty("hijri")]
public List<Hijri> Hijris { get; set; }
[JsonProperty("hijri_events")]
public List<Hijri> HijriEvents { get; set; }
}
公共类同步数据
{
[JsonProperty(“hometab”)]
公共列表hometab{get;set;}
[JsonProperty(“类别”)]
公共列表类别{get;set;}
[JsonProperty(“doaa”)]
公共列表Doaas{get;set;}
[JsonProperty(“hijri”)]
公共列表Hijris{get;set;}
[JsonProperty(“hijri_事件”)]
公共列表HijriEvents{get;set;}
}
接口:
public interface ISynchronizer
{
[Post("/action.php?fn=sync")]
Task<SynchronizerApiResponse> Request([Body(BodySerializationMethod.UrlEncoded)] SynchronizerApiRequest request);
}
公共接口同步器
{
[Post(“/action.php?fn=sync”)]
任务请求([Body(BodySerializationMethod.UrlEncoded)]SynchronizerApiRequest请求);
}
最后是调用api的代码:
public async Task<SynchronizerApiResponse> GetAllData()
{
SynchronizerApiRequest request = new SynchronizerApiRequest()
{
Username = "***",
Password = "***",
LastSyncDate = Methods.IsEmpty(Methods.GetAppProperty(Constants.LAST_SYNC_DATE)) ? ""
: Methods.GetAppProperty(Constants.LAST_SYNC_DATE)
};
syncrhonizer = RestService.For<ISynchronizer>(Constants.BASE_ADDRESS);
SynchronizerApiResponse response = await syncrhonizer.Request(request);
System.Diagnostics.Debug.Write(">>>>>>>>>>>>>>>>>>>>>>>>>>>>." + response.Success + " " + response.Tables.HomeTabs.Count);
return response;
}
公共异步任务GetAllData()
{
SynchronizerAPIReRequest请求=新的SynchronizerAPIReRequest()
{
Username=“***”,
Password=“***”,
LastSyncDate=Methods.IsEmpty(Methods.GetAppProperty(Constants.LAST\u SYNC\u DATE))?“”
:Methods.GetAppProperty(常量、上次同步日期)
};
syncrhonizer=RestService.For(Constants.BASE\u地址);
SynchronizerAppResponse response=等待Synchronizer.Request(请求);
System.Diagnostics.Debug.Write(“>>>>>>>>>>>>>>>>>>>>>>>>>.“+response.Success+”“+response.Tables.HomeTabs.Count”);
返回响应;
}
因此,我使用了newtonsoft json,它起了作用。解决方案是知道如何发送参数,即以何种形式发送。以下是我的解决方案:
string parameters = "username=X&password=Y&last_sync_date=2020-01-01";
// serialize object to json
var content = new StringContent(parameters, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await Client.PostAsync(Constants.GET_ALL_DATA_URI, content);
string clientresult = response.Content.ReadAsStringAsync().Result;
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<SynchronizerApiResponse>(json);
string parameters=“username=X&password=Y&last\u sync\u date=2020-01-01”;
//将对象序列化为json
var content=新的StringContent(参数,Encoding.UTF8,“application/x-www-form-urlencoded”);
var response=wait Client.PostAsync(Constants.GET\u ALL\u DATA\u URI,content);
字符串clientresult=response.Content.ReadAsStringAsync().Result;
response.EnsureSuccessStatusCode();
var json=await response.Content.ReadAsStringAsync();
返回JsonConvert.DeserializeObject(json);
“未检索到响应”-这是什么意思?你有什么反应吗?HTTP返回码是什么?还是超时?“你能让它在邮递员身上工作吗?”杰森说,“它在邮递员身上工作。”。通常它返回success和一个object。在xamarin中,success为false,object为null如果它在Postman中有效,您可以使用Postman生成相应的C#来进行相同的调用。@Jason我解决了它。谢谢你抽出时间