In my database there is a table named index with three fields in it.
ID int Auto_increment,
TITLE text,
CONTENT text;
The Android code below is a packaged class
Packagecom. Example. Jsontest;Import Java. IO. BufferedReader;Import Java. IO. IOException;Import Java. IO. InputStream;Import Java. IO. InputStreamReader;Import Java. IO. Unsupportedencodingexception;Import Java. Util. List;import org. Apache. HTTP. Httpentity;import org. Apache. HTTP. HttpResponse;import org. Apache. HTTP. Namevaluepair;import org. Apache. HTTP. Client. Clientprotocolexception;import org. Apache. HTTP. Client. Entity. Urlencodedformentity;import org. Apache. HTTP. Client. Methods. HttpPost;import org. Apache. HTTP. Impl. Client. Defaulthttpclient;import org. Apache. HTTP. Protocol. HTTP;import org. JSON. Jsonexception;import org. JSON. Jsonobject;Import Android. Util. Log;public class Jsonparser {static InputStream is = null; static Jsonobject jobj = null; static String JSON =""; Public Jsonparser () {} public jsonobject makehttprequest (string url, string method, list<namevaluepair> params) {try {defaulthttpclient httpClient = new Defaulthttpclient (); HttpPost HttpPost = new HttpPost (URL); Using utf-8Encoding the data format HttpPost. Setentity(New Urlencodedformentity (params,http. UTF_8)); HttpResponse HttpResponse = httpClient. Execute(HttpPost); Httpentity httpentity = HttpResponse. GetEntity(); is = httpentity. GetContent(); } catch (Unsupportedencodingexception e) {E. Printstacktrace();} catch (Clientprotocolexception e) {E. Printstacktrace(); } catch (IOException e) {E. Printstacktrace(); } try{BufferedReader reader = new BufferedReader (new InputStreamReader (IS,"UTF-8")); StringBuilder sb = new StringBuilder (); String line = null; while (line = Reader. ReadLine()) = null) {SB. Append(Line +"\ n"); } is. Close(); JSON = SB. toString(); } catch (Exception e) {Log. E("Buffer Error","Error converting result"+ E. toString()); Log. D("JSON"Json. toString()); }//Try parse the string to a JSON object try {jobj = new Jsonobject (JSON); } catch (Jsonexception e) {Log. E("JSON Parser","Error parsing data"+ E. toString()); }//Return JSON String return jobj; } }
Mainactivity
------------------------//Strictmode. Setthreadpolicy(New Strictmode. Threadpolicy. Builder(). Detectdiskreads(). Detectdiskwrites(). Detectnetwork() //or . Detectall() for all detectable problems. Penaltylog(). Build()); Strictmode. Setvmpolicy(New Strictmode. Vmpolicy. Builder(). Detectleakedsqlliteobjects(). Detectleakedclosableobjects(). Penaltylog(). Penaltydeath(). Build()); ------------------------//list<namevaluepair> params = new arraylist<namevaluepair> (); Here you can replace some key values in your own program with String title ="Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";String content ="00000000000000000000000000000000000000000000000";Params. Add(New Basicnamevaluepair ("TITLE",""+title)); Params. Add(New Basicnamevaluepair ("CONTENT",""+ content)); Jsonparser jsonparser = new Jsonparser ();Here is your own path to the PHP file that receives the data String url_up ="http://10.0.2.2/dipingxian/insertbeat.php";try{Jsonobject json = Jsonparser. Makehttprequest(Url_up,"POST", params); Log. V("Uploadsucceed","Uploadsucceed"); }catch (Exception e) {E. Printstacktrace(); }
Success
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android reads and writes MySQL through JSON (write)