我正在尝试在PCL中为xamarin安卓在视觉工作室中使用wcf服务,在调用服务我想得到json响应体。但在这里我不会得到任何响应体,除了200状态。可以请任何人帮助我。任何人感谢建议和帮助。下面是使用的代码。
的Class1.cs
namespace XamarinServiceCall
{
public sealed class Class1 : IRestService
{
public async Task<string> GetData(UserModel model)
{
using (var client = new HttpClient())
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string,string>("domainName",model.domainName ),
new KeyValuePair<string, string>("userName", model.userName),
new KeyValuePair<string, string>("password", model.password),
new KeyValuePair<string, string>("loginFrom", model.loginFrom),
});
var result = await client.PostAsync("http://xamarin-rest-service/LoginService/ValidateLogin", content);
return await result.Content.ReadAsStringAsync();
}
}
}
}
MAinActivity.cs
namespace ServiceSample
{
[Activity(Label = "LayoutSample", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected async override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
XamarinServiceCall.Class1 serviceClass = new XamarinServiceCall.Class1();
var model = new XamarinServiceCall.UserModel { domainName = "domainname" ,userName = "username" , password = "password" ,loginFrom = "App" };
var result = await serviceClass.GetData(model);
button.Text = "get call says: " + result;
}
}
}
下图显示了调试结果变量
的数据以下是我需要发送的内容(URL)并获取响应json body
http://xamarin-rest-service/LoginService/ValidateLogin?domainName = domainname&amp; userName = username&amp; password = password&amp; loginFrom = App
回应邮递员:
“DomainType”:2,“FirstName”:dallia,“状态”:2,“UserId”:3
感谢任何帮助或建议。请帮助我,我对c#和xamarin很新。