我正在使用Visual Studio中的可移植类库中的wcf webservices(POST方法)for xamarin android.App在POST方法中崩溃.Below是我的代码
IN 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 APp中
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 Function时,它显示Expression无效
答案 0 :(得分:0)
您不应该尝试在MainActivity的OnCreate函数内执行任何重要工作,例如调用Web服务。
当然,你不应该让它异步并在其中进行异步调用。请查看Xamarin的例子。
通常,在OnCreate中,您可以进行一些初始化,然后为Application类调用LoadApplication(可以在PCL项目中定义)。