Android reads and writes MySQL through JSON (write)

Source: Internet
Author: User


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)

Related Article

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.