• [Xamarin] 透過WebClient跟網路取得資料 (转帖)


    之前寫過一篇文章,關於在Android上面取得資料 透過GET方式傳資料給Server(含解決中文編碼問題)

    我們來回顧一下 Android 端的Code:

    2013-07-09_105523 

    有沒有超多,如果是在Xaramin下面,真的,把上面不好的回憶給忘了吧,我們來看看在Xamarin下有多溫馨

    首先看一下畫面 Resources > Layout > Main.axml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button
            android:id="@+id/btnGetData1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="取得當麻Facebook資料" />
    </LinearLayout>
     
    2013-07-09_150137

    btnGetData1按鈕按下後,我會讓他去取得 https://graph.facebook.com/donma.hsu 我在Facebook open graph 中的資料.

    直接來看Code :

    using Android.App;
    using Android.Widget;
    using Android.OS;
     
    namespace SampleForWebClient
    {
        [Activity(Label = "測試取得網路資料", MainLauncher = true, Icon = "@drawable/icon")]
        public class Activity1 : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
     
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
     
                var btnGetData1 = FindViewById<Button>(Resource.Id.btnGetData1);
     
                btnGetData1.Click += btnGetData1_Click;
            }
     
            void btnGetData1_Click(object sender, System.EventArgs e)
            {
                var webClient = new System.Net.WebClient();
                //var result = webClient.DownloadString("http://graph.facebook.com/donma.hsu");
                var result = webClient.DownloadString("https://graph.facebook.com/donma.hsu");
                Toast.MakeText(this, result, ToastLength.Long).Show();
            }
     
          
        }
    }
     
    結果:
    Screenshot_2013-07-09-15-00-20
     
    有沒有覺得異常的溫馨+孰悉..
    沒錯,就是WebClient 在Xamarin 官方網站
    直接提到,而且連結就是直接連到MSDN進行參考
     
    2013-07-09_150951
  • 相关阅读:
    vue全局变量定义和修改
    vue格式化显示json数据
    javascript cookie 操作
    bootsrap Collapse用法
    django form 的内置字段类型
    django(channel)到 ubuntu
    django 模型操作
    django 单点登录思路-装饰器
    浏览器右下角提示实现
    django websocket
  • 原文地址:https://www.cnblogs.com/whatthehell/p/3444651.html
Copyright © 2020-2023  润新知