php - Error parsing data org.json.JSONException: Value 403 of type java.lang.Integer cannot be converted to JSONArray -
i following tutorial learn connecting database android app (https://www.youtube.com/watch?v=smvidyck3-k).i followed steps in tutorial.but @ end error on running app (error parsing data org.json.jsonexception: value 403 of type java.lang.integer cannot converted jsonarray) mainactivity.java looks this
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); fetch= (button) findviewbyid(r.id.fetch); text = (textview) findviewbyid(r.id.text); et = (edittext) findviewbyid(r.id.et); fetch.setonclicklistener(this); } class task extends asynctask<string, string, void> { private progressdialog progressdialog = new progressdialog(mainactivity.this); inputstream = null ; string result = ""; protected void onpreexecute() { progressdialog.setmessage("fetching data..."); progressdialog.show(); progressdialog.setoncancellistener(new oncancellistener() { @override public void oncancel(dialoginterface arg0) { task.this.cancel(true); } }); } @override protected void doinbackground(string... params) { string url_select = "http://www.andyapp.byethost4.com/demo.php"; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url_select); arraylist<namevaluepair> param = new arraylist<namevaluepair>(); try { httppost.setentity(new urlencodedformentity(param)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); //read content = httpentity.getcontent(); } catch (exception e) { log.e("log_tag", "error in http connection "+e.tostring()); //toast.maketext(mainactivity.this, "please try again", toast.length_long).show(); } try { bufferedreader br = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = ""; while((line=br.readline())!=null) { sb.append(line+"\n"); } is.close(); result=sb.tostring(); } catch (exception e) { // todo: handle exception log.e("log_tag", "error converting result "+e.tostring()); } return null; } protected void onpostexecute(void v) { // ambil data dari json database try { jsonarray jarray = new jsonarray(result); for(int i=0;i<jarray.length();i++) { jsonobject jasonobject = null; jasonobject = jarray.getjsonobject(i); //get output on screen string name = jasonobject.getstring("name"); string db_detail=""; if(et.gettext().tostring().equalsignorecase(name)) { db_detail = jasonobject.getstring("detail"); text.settext(db_detail); break; } } this.progressdialog.dismiss(); } catch (exception e) { // todo: handle exception log.e("log_tag", "error parsing data "+e.tostring()); } } } @override public void onclick(view v) { // todo auto-generated method stub switch(v.getid()) { case r.id.fetch : new task().execute();break; } }
}
demo.php looks this
<?php mysql_connect("sql306.byethost4.com","user_name","******") or die(mysql_error()); mysql_select_db("db_name"); $sql=mysql_query("select * demo"); while($row=mysql_fetch_assoc($sql)) $output[]=$row; print(json_encode($output)); mysql_close(); ?>
so how overcome error.many help.
you should post json response receive server. error message said had received string "403" means "forbidden". not allowed access php script. in onpostexceute
following line fails:
jsonarray jarray = new jsonarray(result);
Comments
Post a Comment