Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old September 4th, 2012, 05:34 AM   #1 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Nov 2011
Posts: 31
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Default Put Mysql data in array?

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 Code:
<?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.

B8787 is offline  
Reply With Quote
sponsored links
Old September 4th, 2012, 10:24 AM   #2 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,936
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 478 Times in 292 Posts
Default

Take a look at the JSONArray and JSONObject classes.
jonbonazza is offline  
Reply With Quote
Old September 4th, 2012, 10:40 AM   #3 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Nov 2011
Posts: 31
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
B8787 is offline  
Reply With Quote
Old September 5th, 2012, 05:34 AM   #4 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Nov 2011
Posts: 31
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
B8787 is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:52 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.