你的位置:首页 > 软件开发 > 操作系统 > 大叔也说Xamarin~Android篇~调用远程API接口,发POST请求

大叔也说Xamarin~Android篇~调用远程API接口,发POST请求

发布时间:2015-12-02 23:00:03
Xamarin我们在上节已经教大家如何去部署它的环境了,今天来说一个实际的例子,使用android客户调用.net web api的一个接口,并发送POST请求,当服务端回到请求后做出响应,客户端android将响应的内容输出,并跳换到别一个Activity页面,在新视图上加载网 ...

Xamarin我们在上节已经教大家如何去部署它的环境了,今天来说一个实际的例子,使用android客户调用.net web api的一个接口,并发送POST请求,当服务端回到请求后做出响应,客户端android将响应的内容输出,并跳换到别一个Activity页面,在新视图上加载网页内容到webView控件上,整个流程结束!

一 在主页面上添加几个表单元素,账号,密码,登陆按钮,并在Activity类的OnCreate方法中为它们赋值,并添加按钮的click事件

   protected override void OnCreate (Bundle savedInstanceState)    {      base.OnCreate (savedInstanceState);      // 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);            button.Click += delegate {        button.Text = string.Format ("{0} clicks!", count++);      };      Android.Util.Log.Info ("normal", "日志zzl");      var loginBtn = FindViewById<Button> (Resource.Id.loginBtn);      var username = FindViewById<TextView> (Resource.Id.username);      var password = FindViewById<TextView> (Resource.Id.password);      var result = FindViewById<TextView> (Resource.Id.result);      loginBtn.Click += delegate {        string url = "http://api.xuexiba.com/v1/User/Login";        //创建HttpClient(注意传入HttpClientHandler)        using (var http = new HttpClient ()) {          var content = new FormUrlEncodedContent (new Dictionary<string, string> () {             { "username",username.Text },            { "password",password.Text }                      });          var response = http.PostAsync (url, content);          result.Text = response.Result.Content.ReadAsStringAsync ().Result;         Intent intent = new Intent(this, typeof(ViewPageActivity));         StartActivity(intent);         }      };    }

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:大叔也说Xamarin~Android篇~调用远程API接口,发POST请求

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录