connecting android to mysql database -
i have been trying tutorial. doent show errors on emulator url gave appearing!
my connection string was:
public static final string key_121 = "http://10.0.2.2/index.php"; (i used 10.0.2.2 working on local host)
when checked logcat showed me dis:
error parsing data org.json.jsonexception: value <br of type java.lang.string cannot converted jsonarray i dont know whats wrong code. can please me. need on final year project.
this android java code
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.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; import android.widget.linearlayout; import android.widget.textview; public class main extends activity { /** called when activity first created. */ textview txt1, txt2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); // create crude view - should set via layout // resources // since example saves declaring them in xml. txt1 = (textview) findviewbyid(r.id.ip); linearlayout rootlayout = new linearlayout(getapplicationcontext()); txt1 = new textview(getapplicationcontext()); rootlayout.addview(txt1); setcontentview(rootlayout); // set text , call connect function. txt1.settext("connecting..."); // call method run data retreival txt1.settext(getserverdata(key_121)); } public static final string key_121 = "http://10.0.2.2/index.php"; private string getserverdata(string returnstring) { inputstream = null; string result = ""; // year data send arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("year", "1970")); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(key_121); 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); (int = 0; < jarray.length(); i++) { jsonobject json_data = jarray.getjsonobject(i); log.i("log_tag", "longitude: " + json_data.getdouble("longitude") + ", latitude: " + json_data.getdouble("latitude")); // output screen returnstring += "\n\t" + jarray.getjsonobject(i); } } catch (jsonexception e) { log.e("log_tag", "error parsing data " + e.tostring()); } return returnstring; } } this php code
<?php mysql_connect("localhost","root","sugi"); mysql_select_db("android"); $q=mysql_query("select * people birthyear>'".$_request['year']."'"); while($e=mysql_fetch_assoc($q)) $output[]=$e; print(json_encode($output)); mysql_close(); ?> this mysql query
create table people ( id int not null auto_increment primary key , name varchar( 100 ) not null , sex bool not null default '1', birthyear int not null ) this display when type "http://10.0.2.2/index.php" on ie

try , print output server. seems not clear json gets returned. part
value <br of type looks there hard enter somewhere.
Comments
Post a Comment