Latest

    Powered by Blogger.

    Connecting MySQL database with Android via PHP/Java - Tutorial



    In this tutorial , I am gonna show you how you can connect your Android Application to “MySQL” Database through “PHP” and “JAVA“. So lets get started…

    * The Android Device would be connected to PHP Script when a user runs Android Application.
    * A data will be brought out from database by PHP-Script , Encoding to JSON format will be done and would be sent out to device.
    * The Encoded JSON will be arrived to Android Application , Android Application will begin to Parse it and the data would be     displayed to users


    Steps For Evaluation

    ____________________________________________


    Step 1  : First Create a php file and plce it to xamp or wamp server root folder , I am using xamp so i will place it in htdocs folder.
    Step 2 : Now paste the below code in PHP file.

    ____________________________________________


    Code

    ____________________________________________


    <?php
    mysql_connect("localhost","username","password");
    mysql_select_db("Database-Name");
    $sql=mysql_query("select * from Table-Name where EMP_NAME1 like 'KA%'");
    while($row1=mysql_fetch_assoc($sql))
    $out-put[]=$row1;
    print(json_encode($out-put));
    mysql_close();
    ?>

    ____________________________________________


    Step 3 : Now in Android Application
    1) To get data use HTTP-POST.
    2) And now Response will be converted to string by you.
    3) Now the data should be parsed by you and use it as per your intentions.

    ____________________________________________


    Code

    ____________________________________________


    JSONArray j-Array = null;
    
    String result1 = null;
    
    StringBuilder sb1 = null;
    
    InputStream istream = null;

    ArrayList<NameValuePair1> nameValuePairs1 = new ArrayList<NameValuePair1>();
    
    try{
    HttpClient httpclient1 = new DefaultHttpClient();
    HttpPost httppost1 = new HttpPost("http://YOUR_LOCAL_PC_IP_ADDRESS/htdocs/my-File.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
    HttpResponse response1 = httpclient.execute(httppost1);
    HttpEntity entity1 = response1.getEntity();
    is = entity1.getContent();
    }catch(Exception exc){
    Log.exc("logTag", "Error in your HTTP, connection "+exc.toString());
    }
    try{
    BufferedReader reader1 = new BufferedReader(new InputStreamReader(istream,"iso-8859-1"),7);
    sb1 = new StringBuilder();
    sb1.append(reader1.readLine() + "\n");
    
    String line="0";
    while ((line1 = reader.readLine()) != null) {
    sb1.append(line + "\n");
    }
    is.close();
    result1=sb1.toString();
    
    }catch(Exception e){
    Log.exc("logTag", "Error in conversion of result "+exc.toString());
    }

    String name1;
    try{
    j-Array = new JSONArray(result1);
    JSONObject jsonData1=null;
    for(int i=0;i<j-Array.length();i++){
    jsonData = jArray.getJSONObject(i);
    ct_name11=jsonData.getString("NAME1");
    }
    }
    catch(JSONException e12){
    Toast.makeText(getBaseContext(), "Data is not found" ,Toast.LENGTH_LONG).show();
    } catch (ParseException e12) {
    e12.printStackTrace();
    }
    }

    ____________________________________________


    Step 4 : Insert following permission in AndroidManifest.xml.

    ____________________________________________


    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    ____________________________________________



    Reference: You can find the same blog at  http://baidartuts.altervista.org/connecting-mysql-database-with-android-via-php-and-java/
    • Blogger Comments
    • Facebook Comments
    Item Reviewed: Connecting MySQL database with Android via PHP/Java - Tutorial Description: Rating: 5 Reviewed By: Unknown