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>
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 }
运行效果
源码下载:NetJsonList.zip
时间: 2024-11-13 09:50:01