android - MySQL db connection -
i have been searching web connection between android simulator , mysql db.
i've found can't connect directly can via web server. web server handle request android.
i found following code on hello android, don't understand. if run code on simulator, nothing happens; screen stays black. log.i
land, in android screen, error log, or somewhere else?
can me code?
package app.android.ticket; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; 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.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.os.bundle; import android.util.log; public class fetchdata extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //call method run data retreival getserverdata(); } public static final string key_121 = "http://www.jorisdek.nl/android/getallpeoplebornafter.php"; public fetchdata() { log.e("fetchdata", "initialized serverlink "); } private void getserverdata() { inputstream = null; string result = ""; //the year data send arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("year","1980")); //http post try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(key_121); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); }catch(exception e){ log.e("log_tag", "error in http connection "+e.tostring()); } //convert response string try{ bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result=sb.tostring(); }catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } //parse json data try{ jsonarray jarray = new jsonarray(result); for(int i=0;i<jarray.length();i++){ jsonobject json_data = jarray.getjsonobject(i); log.i("log_tag","id: "+json_data.getint("id")+ ", name: "+json_data.getstring("name")+ ", sex: "+json_data.getint("sex")+ ", birthyear: "+json_data.getint("birthyear") ); } }catch(jsonexception e){ log.e("log_tag", "error parsing data "+e.tostring()); } } }
logging messages go in log cat. use logcat reader.
Comments
Post a Comment