Simple Example of Android JSON Parsing

Source: Internet
Author: User

1. The following is a simple JSON parsing demo.

Package com. Sky. Gallery;

Import java. Io. bytearrayoutputstream;
Import java. Io. datainputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java.net. sockettimeoutexception;

Import org. Apache. http. httpentity;
Import org. Apache. http. httpresponse;
Import org. Apache. http. Client. Methods. httpget;
Import org. Apache. http. impl. Client. defaulthttpclient;
Import org. Apache. http. Params. basichttpparams;
Import org. Apache. http. Params. httpconnectionparams;
Import org. Apache. http. Params. httpparams;
Import org. JSON. jsonarray;
Import org. JSON. jsonobject;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;

Public class ctivity extends activity {
// Music

Private Static string game_url = "http://api.k.sohu.com/api/photos/list.go? RT = JSON & categoryid = 2 & pagesize = 4 & p1 = ntc1mzy0otc2nzaxnja0mjuymw % 3d % 3d ";


@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. main_activity );


Startgetgamelistthread ();

}

Public void startgetgamelistthread (){

New thread (){

@ Override
Public void run (){
String jsonstr = NULL;
Int errorcode = 0;

Try {// read data J
Jsonstr = getgamejsonstr (game_url );
Log. I ("test", "jsonstr =" + jsonstr );
// Parseapplist (jsonstr );

} Catch (exception e ){
E. printstacktrace ();
Errorcode = 1;
} Finally {

}

}

}. Start ();
}

Private Static string getgamejsonstr (string URL) throws exception {
Log. I ("test", "getgamejsonstr (). url =" + URL );

String jsonstr = NULL;

Final httpparams httpparameters = new basichttpparams ();
Httpconnectionparams. setconnectiontimeout (HTTP parameters, 6000 );
Httpconnectionparams. setsotimeout (HTTP parameters, 6000 );

Final defaulthttpclient client = new defaulthttpclient (httpparameters );

Final httpget get = new httpget (URL );
Httpresponse resp;

Resp = client.exe cute (get );
Final httpentity entity = resp. getentity ();
Final inputstream is = entity. getcontent ();
Jsonstr = readtoend (is );
Is. Close ();

Return jsonstr;
}

Private Static string readtoend (inputstream input) throws ioexception,
Sockettimeoutexception {
Final datainputstream Dis = new datainputstream (input );
Final byte [] stuff = new byte [2, 1024];
Final bytearrayoutputstream buff = new bytearrayoutputstream ();
Int READ = 0;
While (read = dis. Read (stuff ))! =-1 ){
Buff. Write (stuff, 0, read );
}

Return new string (buff. tobytearray ());
}


Private Static void log (string MSG ){
Log. V ("test", MSG );
}
Private void parseapplist (string jsonstr) throws exception {
Log. V ("test", "sssssssssssssssssssssssssssssssssssss ");
Try {
/*
* Final jsonobject root = new jsonobject (jsonstr); log. I ("test ",
* "Root =" + root );
*/
Jsonobject oo = new jsonobject (jsonstr );
Jsonarray array = oo. getjsonarray ("videos ");
// Final jsonarray array = new jsonarray (jsonstr );
For (INT I = 0; I <20; ++ I ){
// Jsonobject OBJ = array. getjsonobject (I );

Appmodle one = addonegamemodel (array. getjsonobject (I ));
Log. I ("test", "each object's" + I + "" + one. gettitle ());
Log. I ("test", "each object's" + I + "" + one. GETID ());
Log. I ("test", "each object's" + I + "" + one. getartistname ());

}

} Catch (exception e ){
E. printstacktrace ();
}
}

Private Static appmodle addonegamemodel (jsonobject game) throws exception {
Appmodle one = new appmodle ();
/*
One. setid (game. getint ("ID "));
One. settitle (game. getstring ("title "));
One. setartistname (game. getstring ("artistname "));*/
Return one;
}


}

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.