【发布时间】:2016-03-17 10:17:20
【问题描述】:
我在 Visual Studio 的便携式类库中为 xamarin android 使用 wcf webservices(POST 方法)。应用程序在 POST 方法中崩溃。下面是我的代码
在 PCL 中
Class1.cs
namespace XamarinServiceCall
{
public sealed class Class1 : IRestService
{
public async Task<string> GetData(UserModel model)
{
using (var client = new HttpClient())
{
var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
var result = await client.PostAsync("http://xamarin-rest-service/LoginService/ValidateLogin", content);
return await result.Content.ReadAsStringAsync();
}
}
}
}
IRestService.cs
namespace XamarinServiceCall
{
public interface IRestService
{
Task<string> GetData(UserModel model);
}
}
UserModel.cs
public class UserModel
{
public string loginFrom { get; set; }
public string domainName { get; set; }
public string userName { get; set; }
public string password { get; set; }
//I get response of below parameters
public int DomainType { get; set; }
public string FirstName { get; set; }
public int Status { get; set; }
public int UserId { get; set; }
}
在 Xamarin Android 应用程序中
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;
}
}
}
邮递员回复:
当我调试时,我会在结果变量中得到这个响应。
当我指向 postAsync 函数时,它显示表达式无效
【问题讨论】:
-
“崩溃”是什么意思?您在调试器中遇到异常吗?请添加调试器的输出 - 错误消息、堆栈等。