我只是跟着“演练 - 使用 WCF”。但我无法从服务器获取任何数据。
我确实制作了代理并将其添加到 Android 项目中
这是我的活动
using Android.App;
using Android.Widget;
using Android.OS;
using System;
using System.ServiceModel;
using HelloWorldServiceProxy;
using System.Runtime.Serialization;
using static Java.Util.Jar.Attributes;
namespace HelloWorld
{
[Activity(Label = "HelloWorld", MainLauncher = true)]
public class MainActivity : Activity
{
static readonly EndpointAddress Endpoint = new EndpointAddress("http://localhost:8733/Design_Time_Addresses/HelloWorldService/?wsdl");
HelloWorldServiceClient _client;
Button _getHelloWorldDataButton;
TextView _getHelloWorldDataTextView;
Button _sayHelloWorldButton;
TextView _sayHelloWorldTextView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
InitializeHelloWorldServiceClient();
// This button will invoke the GetHelloWorldData - the method that takes a C# object as a parameter.
_getHelloWorldDataButton = FindViewById<Button>(Resource.Id.getHelloWorldDataButton);
_getHelloWorldDataButton.Click += GetHelloWorldDataButtonOnClick;
_getHelloWorldDataTextView = FindViewById<TextView>(Resource.Id.getHelloWorldDataTextView);
// This button will invoke SayHelloWorld - this method takes a simple string as a parameter.
_sayHelloWorldButton = FindViewById<Button>(Resource.Id.sayHelloWorldButton);
_sayHelloWorldButton.Click += SayHelloWorldButtonOnClick;
_sayHelloWorldTextView = FindViewById<TextView>(Resource.Id.sayHelloWorldTextView);
}
void InitializeHelloWorldServiceClient()
{
BasicHttpBinding binding = CreateBasicHttpBinding();
_client = new HelloWorldServiceClient(binding, Endpoint);
}
static BasicHttpBinding CreateBasicHttpBinding()
{
BasicHttpBinding binding = new BasicHttpBinding
{
Name = "basicHttpBinding",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
TimeSpan timeout = new TimeSpan(0, 0, 30);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
return binding;
}
async void GetHelloWorldDataButtonOnClick(object sender, EventArgs e)
{
var data = new HelloWorldData
{
Name = "Mr. Chad",
SayHello = true
};
_getHelloWorldDataTextView.Text = "Waiting for WCF...";
HelloWorldData result;
try
{
result = await _client.GetHelloDataAsync(data);
_getHelloWorldDataTextView.Text = result.Name;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
async void SayHelloWorldButtonOnClick(object sender, EventArgs e)
{
_sayHelloWorldTextView.Text = "Waiting for WCF...";
try
{
var result = await _client.SayHelloToAsync("Kilroy");
_sayHelloWorldTextView.Text = result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
我应该怎么做才能从服务器获取数据......?我做错了什么...?或者演练有什么问题吗?