Android parsing JSON

Source: Internet
Author: User

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

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.