• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Put Mysql data in array?

B8787

Newbie
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:
<?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:
Code:
[{"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.

Code:
ArrayList<String> list = new ArrayList<String>();  
for (??) {  
    list.add(string);  
}

Is it possible to do this on an easy way?

Thanks.
 
Ok, I made the connection through httppost:
But when I run my app, it just crashes from the beginning...
Code:
    // 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());
    		}

I try to get an arraylist imageUrls of titleone, titletwo, titlethree
 
UPDATE
It think that my output was wrong, however, the problem isn't solved yet :(.

Since this worked:

Code:
     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:
Code:
    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... this is my error log file: http://wtrns.fr/JSOMqgHl3c7G2eU
 
Back
Top Bottom