I wanted to fetch the title of an image which is saved in a mysql Db, I found out that you have to do this through Json, right now I am having a .php file:
<?php
mysql_connect("localhost","xx","xx");
mysql_select_db("xx");
$q=mysql_query("SELECT title FROM field_title");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
It outputs as follows:
[{"title":"titleone"},{"title":"titletwo"},{"title":"titlethree"}]
But now I am stuck, I want to put these titles in a simple arraylist but i don't know how to make the connection between the outputted data and the arraylist itself.
ArrayList<String> list = new ArrayList<String>();
for (??) {
list.add(string);
}
Is it possible to do this on an easy way? I have seen this example but I seem not to be able to figure it out:
http://www.helloandroid.com/tutorials/connecting-mysql-database
Thanks.
EDIT
Ok, I was able to make the connection through httppost: But when I run my app, it just crashes from the beginning...
// http post
InputStream inputStream = null;
String result = null;
try {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://www.webmention.com/bosshunting.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try {
JSONArray jArray = new JSONArray(result);
ArrayList imageUrls = new ArrayList();
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
imageUrls.add(json_data.getString("url"));
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
Don't know if I add items to the array on the correct way.. I guess not? I try to get an arraylist imageUrls of titleone, titletwo, titlethree