Xamarin.Android 入門執行個體(1)之擷取與解析JSON

來源:互聯網
上載者:User

標籤:

1.Main.axml 視圖介面

2.視圖代碼

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="fill_parent" 5     android:layout_height="fill_parent" 6     android:minWidth="25px" 7     android:minHeight="25px"> 8     <ListView 9         android:id="@android:id/list"10         android:layout_width="fill_parent"11         android:layout_height="wrap_content" />12 </LinearLayout>
View Code

 

3.MainActivity.cs

  1 using System;  2 using System.Net;  3 using System.IO;  4 using System.Json;  5 using System.Linq;  6 using System.Xml.Linq;  7   8 using Android.App;  9 using Android.Content; 10 using Android.Runtime; 11 using Android.Views; 12 using Android.Widget; 13 using Android.OS; 14  15 namespace NetJsonList 16 { 17     [Activity(Label = "NetJsonList", MainLauncher = true, Icon = "@drawable/icon")] 18     public class MainActivity : ListActivity 19     { 20         class Test : Java.Lang.Object 21         { 22             public string[] Results { get; set; } 23         } 24  25         Test t; 26  27         protected override void OnCreate(Bundle bundle) 28         { 29             base.OnCreate(bundle); 30             SetContentView(Resource.Layout.Main); 31             LoadXamarin(); 32         } 33  34         //重寫該方法 35         public override Java.Lang.Object OnRetainNonConfigurationInstance() 36         { 37             return t; 38         } 39  40         public void LoadXamarin() 41         { 42             t = LastNonConfigurationInstance as Test; 43             ////判斷是否存在之前的狀態 44             if (t != null) 45             { 46                 ListAdapter = new ArrayAdapter<string>(this, Resource.Id.listView1, t.Results); 47             } 48             else 49             { 50  51                 //JSON請求URL 52                 string url = "http://192.168.1.2/Country/OaAreaByCountryId?countryId=21&pid=76"; 53                 /* 54                  {"stats":1,"result":[{"pid":76,"id":77,"name":"麗水市","py":"L","items":[{"iskc":true,"ids":"","peisongurl":"","id":78,"name":"蓮都區","py":"L"},{"iskc":true,"ids":"","peisongurl":"","id":79,"name":"龍泉市","py":"L"},{"iskc":true,"ids":"","peisongurl":"","id":80,"name":"青田縣","py":"Q"},{"iskc":true,"ids":"","peisongurl":"","id":81,"name":"縉雲縣","py":"J"},{"iskc":true,"ids":"","peisongurl":"","id":82,"name":"遂昌縣","py":"S"},{"iskc":true,"ids":"","peisongurl":"","id":83,"name":"松陽縣","py":"S"},{"iskc":true,"ids":"","peisongurl":"","id":84,"name":"雲和縣","py":"Y"},{"iskc":true,"ids":"","peisongurl":"","id":85,"name":"慶元縣","py":"Q"},{"iskc":true,"ids":"","peisongurl":"","id":241,"name":"景寧佘族自治縣","py":"J"}]}]} 55                  */ 56                 //建立一個請求 57                 var httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); 58                 //擷取響應 59                 // var httpRes = (HttpWebResponse)httpReq.GetResponse(); 60                 //讀取流文本 61                 //string text = new StreamReader(httpRes.GetResponseStream()).ReadToEnd(); 62                 //將流中的基於文本的 JSON 還原序列化為 JSON CLR 類型 63                 //var text = (JsonObject)JsonObject.Load(httpRes.GetResponseStream()); 64                 //測試 65                 //var result = new string[] { "1", "2", "3", "4" }; 66                 //返回包含 JsonObject 中的鍵的集合 67                 //var result = text.Keys.ToList(); 68                 //適配列表 視圖ListView控制項id="@android:id/list" 69                 //ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, result); 70  71                 httpReq.BeginGetResponse(new AsyncCallback(ReadXamarin), httpReq); 72             } 73         } 74  75         //非同步回調方法 76         public void ReadXamarin(IAsyncResult asyn) 77         { 78             var httpReq = (HttpWebRequest)asyn.AsyncState; 79  80             //擷取響應 81             using (var httpRes = (HttpWebResponse)httpReq.EndGetResponse(asyn)) 82             { 83                 //判斷是否成功擷取響應 84                 if (httpRes.StatusCode == HttpStatusCode.OK) 85                 { 86                     //讀取響應 87                     //var text = new StreamReader(httpRes.GetResponseStream()).ReadToEnd(); 88                     var text = (JsonObject)JsonObject.Load(httpRes.GetResponseStream()); 89                     //返回包含 JsonObject 中的鍵的集合 90                     var result = text.Keys.ToList(); 91                     //適配列表 視圖ListView控制項id="@android:id/list" 92                     //ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, result); 93                     //切換到UI線程,否則無法對控制項進行操作 94                     RunOnUiThread(() => 95                     { 96                         ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, result); 97                     }); 98                 } 99             }100         }101     }102 }
View Code

運行效果

 

源碼下載:NetJsonList.zip

Xamarin.Android 入門執行個體(1)之擷取與解析JSON

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.