Android : New android enthusiast "JSONException: Value of type java.lang.String cannot be converted to JSONObject"

on Saturday, January 31, 2015


I'm new to android, but I found an app that would go great with a web scraper that I've been working very hard on and filled a database with. This is a basic app that retrieves values from a MySQL database using a PHP script and displays them on your android device.


here is the PHP:



<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=beanbag', **********, ********);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}

$sql=mysql_query("select * from Recipes");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>


The original code used mysql_connect but I implemented PDO instead.


Here is the Java:



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;
//text_1 = (TextView)findViewById(R.id.txt1);
Jasonobject = Jarray.getJSONObject(i);

//get an output on the screen
//String id = Jasonobject.getString("id");
String name = Jasonobject.getString("Title");
String db_detail="";

if(et.getText().toString().equalsIgnoreCase(name)) {
db_detail = Jasonobject.getString("ingredient");
text.setText(db_detail);
break;
}
//text_1.append(id+"\t\t"+name+"\t\t"+password+"\t\t"+"\n");

}
this.progressDialog.dismiss();

} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error parsing data "+e.toString());
}
}


I can't post pics yet but Logcat says this:


JSONException: Value of type java.lang.String cannot be converted to JSONObject


Thanks for any time you would spend on this. Many other people in the comments section on the tekart blog are getting this same error but there hasn't been a good solution posted. I tried using {left brace and }right brace solution from this stackoverflow question, but I don't think that's it. I'm gaining a lot of confidence in my programming skills, but I hit a wall with this problem. Let me know if I need to post any more code or anything. Thanks again!


0 comments:

Post a Comment