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);
$results=$conn->query('SELECT * FROM Recipes');
while ($results = $row->fetch(PDO::FETCH_ASSOC)){
$output[]=$row;
print(json_encode($output));
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
$conn = null;
?>
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());
}
}
Logcat says:
Error parsing data org.json.JSONException: Value br of type java.lang.String cannot be converted to JSONArray
I tried using {left brace and }right brace solution from this stackoverflow question, but I don't think that's it.