0

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

s0169720
  • 1
  • 2

2 Answers2

0

hellow i have check your json response here Json its true. i think you make mistack here

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("title")); }
         } catch (JSONException e) 
         { Log.e("log_tag", "Error parsing data " + e.toString()); }

    }
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
0

It seems that my output was wrong.

Since this worked:

   List<String> list = new ArrayList<String>();
        String string1 = "https://www.link.com/file1.jpg";
        String string2 = "https://www.link.com/file2.jpg";
        list.add(string1);
        list.add(string2);
        imageUrls = (String[]) list.toArray(new String[0]);

I tried the following:

InputStream inputStream = null;
        String result = null;
        try {
            DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost("http://www.file.com/connect.php");
        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());
        }

        try {
            JSONArray jArray = new JSONArray(result);
         List<String> list = new ArrayList<String>();
         for (int i = 0; i < jArray.length(); i++)
         { JSONObject json_data = jArray.getJSONObject(i); 

            list.add(json_data.getString("url"));

         } imageUrls = (String[]) list.toArray(new String[0]);
        }catch (JSONException e) 
         { Log.e("log_tag", "Error parsing data " + e.toString()); }

    }

But unfortunately this also gives an error... Seems the same error as in my logfile above..

s0169720
  • 1
  • 2