FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


connect android app to mySQL database(5 steps)





abdul91
1-set the INTERNET permission in you manifest
Code:
<uses-permission android:name="android.permission.INTERNET" />


2-make a class to make an http request
Code:
---------------------------------------public class JSONfunctions {
  public static JSONObject getJSONfromURL(String url) {
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    // Download JSON data from URL
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // Convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), );
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    try {

        jArray = new JSONObject(result);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    return jArray;
  }
}
-------------------------------------


3-in your MainActivity class, call your JsonFunctions class and pass the URL
EX:
Code:
--------------
JSONObject jsonobject;
jsonobject = JSONfunctions.getJSONfromURL("http://database url");
--------------


4-finally store the JSON tags in an ArrayList

5-show them where you want

thank you for visiting Rolling Eyes Rolling Eyes
Marcuzzo
dude,
The title says 'connect android app to mySQL database'
This code doesn't connect to a database but it pulls JSON data from a webservice.
Also, you may want to consider doing this asynchronously to keep your app responsive. you should put this code in a class that inherits from AsyncTask


a few extra hints:
1. Wrap your code in the CODE tags ( gives you better points Wink )
2. if you copy paste code from another website, please mention it's source.

btw: making tutorials is nice and all but there is a place for those posts. which is unfortunate for most people that are here just for the points. I'm glad that the tutorial forum has points disabled, or we would find posts on how to properly wipe our arses.
codersfriend
I think you are not actually "connecting" but just fetching external data.
Anyway I think you're just trying to gather points. Please use the code block tag for your source codes
Marcuzzo
codersfriend wrote:
I think you are not actually "connecting" but just fetching external data.
Anyway I think you're just trying to gather points. Please use the code block tag for your source codes

source codes, codes, codes, code, co, c

Twisted Evil
Related topics
Connect to MySql database
Connecting to a MySQL database using Python
Fixing 'Can't Connect to database' errors with phpBB.
how to connect php with mysql?
PHPBB MYSQL Database Question.
Problem connecting to MySQL database with PHP [RESOLVED]
Finding the newest entry in a mysql database?
Connecting to 2 different Mysql database and tables?
Maximum MYSQL database size
mysql connection question
Cron Job to Backup MySQL Database
Mysql database problem
MySQL database for a forum
Mysql database size?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.