Saturday, May 26, 2012

Android and mySql Interaction

Recently I am working on interacting android with Mysql where I have to save every message of user into the mysql server form android.
So I need to connect mySql database with android activity. Though it is so easy to establish connection with mysql. But for the first time it will take time for the novice programmer.
Because of time saving I am here writing the method how to connect mysql and android perfectly and in proper way.
Remember I am just trying to inform you the method how to do it. I am not writing all the code.Sorry for the code formatting it is the problem of writing in blog that is doesn't if you get your solution.

You have sent message any simple message from edittext field from main activity to this class
// serverActivity.java

public class serverActivity extends MainActivity{

static InputStream inputStream;
static ArrayList<NameValuePair> nameValuePairs;
/**
* Sent message to the server
* @param msg
*/
public static void insertMsgIntoServer(String msg) {

try {
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("UserMessage", msg)); //Passing user message with Index Value "UserMessage"

httpClient.httpConnection(nameValuePairs,inputStream);    //Calling httpConnection passing arrayList and InputStream
Log.d("<<<-Inside insertMsgToServer->>>", "Success in saving msg into server");

}catch(Exception ex) {
Log.d("<<<-Inside insertMsgToServer->>>", "Error in saving msg into server");
}//end of exception handling
}//end of insertMsg()
}//end of main class

httpClient.java here you have to sent the nameValuePair arrayList and inputStream for sending data into the mysql.

//httpClient.java

public class httpClient {
/**
* @param nameValuePairs passing arraylist with all information added
* @param inputStream passing inputStream from user request
*/
public static void httpConnection(ArrayList<NameValuePair> nameValuePairs,java.io.InputStream inputStream){
try{
HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost("http://10.0.2.2/android/msg.php");  
//when you want the emulator client to contact a server running on the same host, use the alias 10.0.2.2 to refer to the host computeres loopback interface. From the emulators perspective, localhost (127.0.0.1) refers to its own loopback interface.So localhost or 127.0.0.1 will not work here.

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    //Passing nameValuePairs ArrayList to the server


HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
Log.d("<<<-http_log_tag->>>","Succes in Http Connection");

}catch(Exception e){

Log.e("<<<-http_log_tag->>>","Error in Http Connection" + e.toString());

}//end of exception handling

}//end of httpConnection()

}//end of main class

So here is the end of android portion. Now you have to get this message from "http://10.0.2.2/android/msg.php" . So here is the code how will do that to insert the message into your mysql database.

//msg.php
<?php
$ms=$_REQUEST["UserMessage"];

mysql_connect("localhost","root","");
mysql_select_db("android");

$query = mysql_query("insert into msg (message)values('$ms')");

if($query>0){
echo "Data Inserted Seccesfully";
}else{
echo "Error Occured.";
        }
?>

If you have done all the things above then start your xampp server then run your AVD emulator. After writing any message in edittext box  and pressing the button to chat that will execute event which will sent message to the serverActivity.java class. After processing here message will sent to perform the activity of httpClient which will connect with the server and execute the msg.php . Server will insert your message to your database. Check your database you will get your message into database.

Thanks everyone. If have any problem just comment your problem hope you will get your solution as soon as possible.
Happy Coding.

6 comments:

  1. hello how can i do the opposite? to get a response from sql server

    ReplyDelete
    Replies
    1. Thirsty To Know And Learn: Android And Mysql Interaction >>>>> Download Now

      >>>>> Download Full

      Thirsty To Know And Learn: Android And Mysql Interaction >>>>> Download LINK

      >>>>> Download Now

      Thirsty To Know And Learn: Android And Mysql Interaction >>>>> Download Full

      >>>>> Download LINK kX

      Delete
  2. how i can get data from the sql server ?

    ReplyDelete
  3. Can i get Complete code.

    i m getting error as
    Error in http connection android.os.NetworkOnMainThreadException

    Error converting result java.lang.NullPointerException: lock == null

    ReplyDelete
  4. Thirsty To Know And Learn: Android And Mysql Interaction >>>>> Download Now

    >>>>> Download Full

    Thirsty To Know And Learn: Android And Mysql Interaction >>>>> Download LINK

    >>>>> Download Now

    Thirsty To Know And Learn: Android And Mysql Interaction >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete