php - Error Parsing Json from Mysql -
hi have code retrieve mysql database there error when comes parsing info. think because information in 1 string, @override protected void doinbackground(string... params) {
string result = ""; inputstream = null; //http post try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://cs1.ucc.ie/~am32/getdb.php"); //httppost.setentity(new urlencodedfromentity(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(); sb.append(reader.readline() + "\n"); string line = null; while ((line = reader.readline()) != null) { sb.append(line); } is.close(); result=sb.tostring(); log.i("json string", result); } catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } //parse json data try{ list<string> mylist= new arraylist<string>(arrays.aslist(result.split(" "))); jsonarray jarray = new jsonarray(mylist); system.out.println("length"+ jarray.length()); log.d("db","length"+jarray.length()); for(int i=0; i<jarray.length(); i++){ jsonobject json_data = null; json_data = jarray.getjsonobject(i); int id = +1; //string title = json_data.getstring("title"); double latitude = json_data.getdouble("lat"); double longitude = json_data.getdouble("lon"); //adds proximity poi's proxalert inst = new proxalert(); inst.addproximityalert(latitude,longitude, id); // inst.savecoordinatesinpreferences((float)latitude, (float)longitude); //prints logcat system.out.println(id+"&"+latitude+"&"+longitude); } } catch(jsonexception e){ log.e("log_tag", "error parsing data "+e.tostring()); log.e("log_tag","failed data as:\n"+result); } return null; } and logcat reads
09-04 19:13:13.997: e/log_tag(28100): error parsing data org.json.jsonexception: value @ 0 of type java.lang.string cannot converted jsonobject
in code, doing http request (http://cs1.ucc.ie/~am32/getdb.php) , getting content this
51.899429 -8472801 51.898525 -8.472176 51.899522 -8.47293 51.899495 -8.472844 51.899456 -8.472823 51.899628 -8.473016 51.899442 -8.47278 51.89931 -8.472587 51.808514 -8.472248 51.899174 -8.472649 51.899469 -8.472823 51.8932045 -8.500971 51.893623 -8.501472 this not valid json.
for check json validation use jsonlint .
[edit] please read json document first better understanding. here android-json-parsing-tutorial tutorial explains json parsing example.
Comments
Post a Comment