Xamarin.android get and parse json for getting Started instance (1)

Source: Internet
Author: User

1.main.axml View Interface

2. View Code

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns: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     <ListView9         Android:id= "@android: Id/list"Ten Android:layout_width= "Fill_parent" One Android:layout_height= "Wrap_content" /> A </LinearLayout>
View Code

3.mainactivity.cs

1 usingSystem;2 usingSystem.Net;3 usingSystem.IO;4 usingSystem.json;5 usingSystem.Linq;6 usingSystem.Xml.Linq;7 8 usingAndroid.app;9 usingandroid.content;Ten usingAndroid.runtime; One usingandroid.views; A usingAndroid.widget; - usingAndroid.os; -  the namespacenetjsonlist - { -[Activity (Label ="netjsonlist", Mainlauncher =true, Icon ="@drawable/icon")] -      Public classmainactivity:listactivity +     { -         classTest:Java.Lang.Object +         { A              Public string[] Results {Get;Set; } at         } -  - Test t; -  -         protected Override voidOnCreate (Bundle bundle) -         { in             Base. OnCreate (bundle); - Setcontentview (Resource.Layout.Main); to Loadxamarin (); +         } -  the         //Override this method *          Public OverrideJava.Lang.Object onretainnonconfigurationinstance () $         {Panax Notoginseng             returnT; -         } the  +          Public voidLoadxamarin () A         { thet = lastnonconfigurationinstance asTest; +             ////Determine if there is a previous state -             if(t! =NULL) $             { $ListAdapter =Newarrayadapter<string> ( This, Resource.Id.listView1, t.results); -             } -             Else the             { - Wuyi                 //JSON request URL the                 stringURL ="http://192.168.1.2/Country/OaAreaByCountryId?countryId=21&pid=76"; -                 /* Wu {"Stats": 1, "result": [{"pid": "", "id": "," "Name": "Lishui", "py": "L", "items": [{"ISKC": True, "IDs": "," Peisongu RL ":", "id": +, "name": "Liandu District", "py": "L"},{"ISKC": True, "IDs": "", "Peisongurl": "", "id": +, "name": "Longquan", "py": "L"},{" ISKC ": True," IDs ":" "," Peisongurl ":" "," id ": +," name ":" Qingtian County "," py ":" Q "},{" ISKC ": True," IDs ":" "," Peisongurl ":" "," id " : Bayi, "name": "Jinyun County", "py": "J"},{"ISKC": True, "IDs": "", "Peisongurl": "", "id":, "name": "Suichang County", "py": "S"},{"ISKC": true, " IDs ":", "Peisongurl": "", "id": "Songyang County", "py": "S"},{"ISKC": True, "IDs": "", "Peisongurl": "", "id": "", "" Name ":" Yunhe County "," py ":" Y "},{" ISKC ": True," IDs ":" "," Peisongurl ":" "," id ":", "" Name ":" Qingyuan County "," py ":" Q "},{" ISKC ": True," IDs ":" "," Peisongurl ":", "id": 241, "name": "Jingning County", "py": "J"}]}]} -                  */ About                 //Create a request $                 varHttpreq = (HttpWebRequest) httpwebrequest.create (NewUri (URL)); -                 //Get Response -                 //var httpres = (HttpWebResponse) httpreq.getresponse (); -                 //Reading stream Text A                 //string text = new StreamReader (Httpres.getresponsestream ()). ReadToEnd (); +                 //deserializes text-based JSON from a stream into a JSON CLR type the                 //var text = (jsonobject) jsonobject.load (Httpres.getresponsestream ()); -                 //Test $                 //var result = new string[] {"1", "2", "3", "4"}; the                 //returns a collection containing the keys in the Jsonobject the                 //var result = text. Keys.tolist (); the                 //Adapter List View ListView control Id= "@android: Id/list" the                 //listadapter = new Arrayadapter<string> (this, Android.Resource.Layout.SimpleListItem1, result); -  inHttpreq.begingetresponse (NewAsyncCallback (Readxamarin), httpreq); the             } the         } About  the         //Asynchronous Callback methods the          Public voidReadxamarin (IAsyncResult Asyn) the         { +             varHttpreq =(HttpWebRequest) Asyn. asyncstate; -  the             //Get ResponseBayi             using(varHttpres =(HttpWebResponse) httpreq.endgetresponse (Asyn)) the             { the                 //determine if a response was successfully obtained -                 if(Httpres.statuscode = =Httpstatuscode.ok) -                 { the                     //Read Response the                     //var text = new StreamReader (Httpres.getresponsestream ()). ReadToEnd (); the                     varText =(Jsonobject) jsonobject.load (Httpres.getresponsestream ()); the                     //returns a collection containing the keys in the Jsonobject -                     varresult =text. Keys.tolist (); the                     //Adapter List View ListView control Id= "@android: Id/list" the                     //listadapter = new Arrayadapter<string> (this, Android.Resource.Layout.SimpleListItem1, result); the                     //switch to the UI thread, otherwise the control cannot be manipulated94Runonuithread (() = the                     { theListAdapter =Newarrayadapter<string> ( This, Android.Resource.Layout.SimpleListItem1, result); the                     });98                 } About             } -         }101     }102}
View Code

Run effect

SOURCE Download: Netjsonlist.zip

Xamarin.android get and parse json for getting Started instance (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.