zoukankan      html  css  js  c++  java
  • Xamarin.Android 入门实例(1)之获取与解析JSON

    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

  • 相关阅读:
    springcloud相关组件使用时的jar包
    day62-django-反向解析、路由分发、名称空间、伪静态、视图层(三板斧、JsonResponse、form表单上传文件、request对象方法、FBV与CBV)
    day61-django-数据的查改删、创建表关系 、请求生命周期流程图、路由层(路由匹配 无名分组 有名分组 无名有名是否可以混合使用 反向解析)
    AcWing487. 金明的预算方案题解(DP,分组背包)
    day60-django-静态文件配置、request方法、链接数据库、ORM操作
    day59-django-写一个简易版本的web框架、jinja2、web框架请求流程图、框架介绍、django基本操作
    day58-jQuery事件的阻止、委托、页面加载、动画、前端框架bootstrap、搭建图书管理系统
    day57-jQuery练习、操作标签、事件
    day56-js原生事件绑定-jQuery导入、查找标签
    day55-前端js-BOM与DOM操作
  • 原文地址:https://www.cnblogs.com/mschen/p/4262057.html
Copyright © 2011-2022 走看看