The point is to turn on network permissions
The difficulty is calling the function
Turn on network permissions
</ Application > < android:name= "Android.permission.INTERNET"></ Uses-permission></manifest>
Functions for reading JSON
Public Staticstring Get (String urlstring) {/*try{HttpGet request = new HttpGet (urlstring); String result=gethttpclient (). Execute (request,new basicresponsehandler ()); return result; }catch (IOException e) {throw e; }*/String Result=""; BufferedReader in=NULL; Try{HttpClient Client=Newdefaulthttpclient (); HttpGet Request=NewHttpGet (urlstring); HttpResponse Response=Client.execute (Request); Inch=NewBufferedReader (NewInputStreamReader (Response.getentity (). GetContent ())); StringBuffer SB=NewStringBuffer (""); String Line=""; String NL=system.getproperty ("Line.separator"); //String nl= ""; while((Line=in.readline ())! =NULL) {sb.append ( line+NL); } in.close (); Result=sb.tostring (); Result=jsonfilter (Result); } Catch(Exception e) {e.printstacktrace (); } finally{ if(in!=NULL){ Try{in.close (); }Catch(IOException e) {e.printstacktrace (); } } } returnresult; } /** Filter JSON strings to prevent garbled and black boxes*/ Public Staticstring Jsonfilter (String jsonstr) {returnJsonstr.substring (Jsonstr.indexof ("{")). Replace ("\ r \ n", "\ n")); }
Calling functions
/***************************************************** is mostly click event *********************************************/@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); FinalButton button1 = (button) Findviewbyid (R.id.button1);//final get handle get handle FinalTextView TextView =(TextView) Findviewbyid (R.ID.TEXTVIEW1); Button1.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {textview.settext (Get ("Http://www.weather.com.cn/data/cityinfo/101010100.html")); } }); }
Android parsing JSON