Xamarin.Android 入门实例(1)之获取与解析JSON

时间:2024-12-11 21:06:14

1.Main.axml 视图界面

Xamarin.Android 入门实例(1)之获取与解析JSON

2.视图代码

 <?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"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

3.MainActivity.cs

 using System;
using System.Net;
using System.IO;
using System.Json;
using System.Linq;
using System.Xml.Linq; using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS; namespace NetJsonList
{
[Activity(Label = "NetJsonList", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : ListActivity
{
class Test : Java.Lang.Object
{
public string[] Results { get; set; }
} Test t; protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
LoadXamarin();
} //重写该方法
public override Java.Lang.Object OnRetainNonConfigurationInstance()
{
return t;
} public void LoadXamarin()
{
t = LastNonConfigurationInstance as Test;
////判断是否存在之前的状态
if (t != null)
{
ListAdapter = new ArrayAdapter<string>(this, Resource.Id.listView1, t.Results);
}
else
{ //JSON请求URL
string url = "http://192.168.1.2/Country/OaAreaByCountryId?countryId=21&pid=76";
/*
{"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"}]}]}
*/
//创建一个请求
var httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
//获取响应
// var httpRes = (HttpWebResponse)httpReq.GetResponse();
//读取流文本
//string text = new StreamReader(httpRes.GetResponseStream()).ReadToEnd();
//将流中的基于文本的 JSON 反序列化为 JSON CLR 类型
//var text = (JsonObject)JsonObject.Load(httpRes.GetResponseStream());
//测试
//var result = new string[] { "1", "2", "3", "4" };
//返回包含 JsonObject 中的键的集合
//var result = text.Keys.ToList();
//适配列表 视图ListView控件id="@android:id/list"
//ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, result); httpReq.BeginGetResponse(new AsyncCallback(ReadXamarin), httpReq);
}
} //异步回调方法
public void ReadXamarin(IAsyncResult asyn)
{
var httpReq = (HttpWebRequest)asyn.AsyncState; //获取响应
using (var httpRes = (HttpWebResponse)httpReq.EndGetResponse(asyn))
{
//判断是否成功获取响应
if (httpRes.StatusCode == HttpStatusCode.OK)
{
//读取响应
//var text = new StreamReader(httpRes.GetResponseStream()).ReadToEnd();
var text = (JsonObject)JsonObject.Load(httpRes.GetResponseStream());
//返回包含 JsonObject 中的键的集合
var result = text.Keys.ToList();
//适配列表 视图ListView控件id="@android:id/list"
//ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, result);
//切换到UI线程,否则无法对控件进行操作
RunOnUiThread(() =>
{
ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, result);
});
}
}
}
}
}

Xamarin.Android 入门实例(1)之获取与解析JSON

运行效果

Xamarin.Android 入门实例(1)之获取与解析JSON

源码下载:NetJsonList.zip