1. Check out our companion app, Forums for Android! Download from Google Play

Put Mysql data in array?

Discussion in 'Application Development' started by B8787, Sep 4, 2012.

  1. B8787

    B8787 Active Member
    Thread Starter
    15

    Nov 15, 2011
    31
    0
    15
    Hi all,

    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:
    1. <?php
    2. mysql_connect("localhost","xx","xx");
    3.  
    4. $q=mysql_query("SELECT title FROM field_title");
    5. while($e=mysql_fetch_assoc($q))
    6.         $output[]=$e;
    7.  
    8. print(json_encode($output));
    9.  
    10. ?>
    It outputs as follows:
    Code (Text):
    1.  
    2. [{"title":"titleone"},{"title":"titletwo"},{"title":"titlethree"}]
    3.  
    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.

    Code (Text):
    1. ArrayList<String> list = new ArrayList<String>();  
    2. for (??) {  
    3.     list.add(string);  
    4. }  
    5.  
    Is it possible to do this on an easy way?

    Thanks.
     

    Advertisement

  2. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    459
    163
    Male
  3. B8787

    B8787 Active Member
    Thread Starter
    15

    Nov 15, 2011
    31
    0
    15
    Ok, I made the connection through httppost:
    But when I run my app, it just crashes from the beginning...
    Code (Text):
    1.  
    2.     // http post
    3.             InputStream inputStream = null;
    4.             String result = null;
    5.             try {
    6.                 DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    7.             HttpPost httppost = new HttpPost("http://www.webmention.com/bosshunting.php");
    8.             // Depends on your web service
    9.             httppost.setHeader("Content-type", "application/json");
    10.    
    11.            
    12.             HttpResponse response = httpclient.execute(httppost);          
    13.             HttpEntity entity = response.getEntity();
    14.             } catch (Exception e) {
    15.                 Log.e("log_tag", "Error in http connection " + e.toString());
    16.             }
    17.             // convert response
    18.             try {
    19.                 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
    20.                 StringBuilder sb = new StringBuilder();
    21.    
    22.                 String line = null;
    23.                 while ((line = reader.readLine()) != null)
    24.                 {
    25.                     sb.append(line + "\n");
    26.                 }
    27.                 result = sb.toString();
    28.                
    29.             } catch (Exception e) {
    30.                 Log.e("log_tag", "Error converting result " + e.toString());
    31.             }
    32.    
    33.            
    34.            
    35.             // parse json data
    36.             try {
    37.                 JSONArray jArray = new JSONArray(result);
    38.                 ArrayList imageUrls = new ArrayList();
    39.                 for (int i = 0; i < jArray.length(); i++) {
    40.                     JSONObject json_data = jArray.getJSONObject(i);
    41.                    
    42.                     imageUrls.add(json_data.getString("url"));
    43.                 }
    44.    
    45.             } catch (JSONException e) {
    46.                 Log.e("log_tag", "Error parsing data " + e.toString());
    47.             }
    48.  
    I try to get an arraylist imageUrls of titleone, titletwo, titlethree
     
  4. B8787

    B8787 Active Member
    Thread Starter
    15

    Nov 15, 2011
    31
    0
    15
    UPDATE
    It think that my output was wrong, however, the problem isn't solved yet :(.

    Since this worked:

    Code (Text):
    1.      List<String> list = new ArrayList<String>();
    2.             String string1 = "https://www.link.com/file1.jpg";
    3.             String string2 = "https://www.link.com/file2.jpg";
    4.             list.add(string1);
    5.             list.add(string2);
    6.             imageUrls = (String[]) list.toArray(new String[0]);
    7.  
    I tried the following:
    Code (Text):
    1.  
    2.     InputStream inputStream = null;
    3.             String result = null;
    4.             try {
    5.                 DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    6.             HttpPost httppost = new HttpPost("http://www.file.com/connect.php");
    7.             httppost.setHeader("Content-type", "application/json");
    8.    
    9.             HttpResponse response = httpclient.execute(httppost);          
    10.             HttpEntity entity = response.getEntity();
    11.             } catch (Exception e) {
    12.                 Log.e("log_tag", "Error in http connection " + e.toString());
    13.             }
    14.             // convert response
    15.             try {
    16.                 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
    17.                 StringBuilder sb = new StringBuilder();
    18.    
    19.                 String line = null;
    20.                 while ((line = reader.readLine()) != null)
    21.                 {
    22.                     sb.append(line + "\n");
    23.                 }
    24.                 result = sb.toString();
    25.                
    26.             } catch (Exception e) {
    27.                 Log.e("log_tag", "Error converting result " + e.toString());
    28.             }
    29.            
    30.             try {
    31.                 JSONArray jArray = new JSONArray(result);
    32.              List<String> list = new ArrayList<String>();
    33.              for (int i = 0; i < jArray.length(); i++)
    34.              { JSONObject json_data = jArray.getJSONObject(i);
    35.        
    36.                 list.add(json_data.getString("url"));
    37.                    
    38.              } imageUrls = (String[]) list.toArray(new String[0]);
    39.             }catch (JSONException e)
    40.              { Log.e("log_tag", "Error parsing data " + e.toString()); }
    41.    
    42.         }
    43.  
    But unfortunately this also gives an error... this is my error log file: http://wtrns.fr/JSOMqgHl3c7G2eU
     

Share This Page

Loading...