Android Mysql

Posted: October 14, 2012 in Android

Android <=> Mysql

Main.java

——————————————

package com.example.databasetest_1;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private EditText email;
private EditText pwd;
private Button submit;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

email = (EditText)findViewById(R.id.editTextEmail);
pwd = (EditText)findViewById(R.id.editTextPassword);
submit = (Button)findViewById(R.id.buttonSubmit);

submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try{
Toast.makeText(getApplicationContext(), “test”, Toast.LENGTH_SHORT).show();
Map<String, String> map = new HashMap<String, String>();
String emailString = email.getText().toString();
String pwdString = pwd.getText().toString();
map.put(“email”, emailString);
map.put(“pwd”, pwdString);
boolean result = submitDataByHttpClientDoPost(map, “http://10.0.2.2/android_con.php&#8221;);

if(result) {
Toast.makeText(getApplicationContext(), “Data Inserted.”, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), “Failed”, Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public Boolean submitDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception {

HttpClient hc = new DefaultHttpClient();

HttpPost request = new HttpPost(path);

List<NameValuePair> parameters = new ArrayList<NameValuePair>();

for(Map.Entry<String, String> entry:map.entrySet()) {
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue());
parameters.add(nvp);
}

HttpEntity entity = new UrlEncodedFormEntity(parameters, “UTF-8”);

request.setEntity(entity);

HttpResponse response = hc.execute(request);

if(response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
return true;
}
else {
return false;
}
}
}

 

main.xml

—————————————–

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<EditText
android:id=”@+id/editTextEmail”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Email” >

<requestFocus />
</EditText>

<EditText
android:id=”@+id/editTextPassword”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Password”
android:inputType=”textPassword” />

<Button
android:id=”@+id/buttonSubmit”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Submit” />

</LinearLayout>

 

android_con.php

——————————————

<?php

$con = mysql_connect(“localhost”, “root”, “password”);

if(!$con) {
die(‘Could not connect: ‘.mysql_connect);
}
else {

mysql_select_db(“database”, $con);

//get the android sending data
$str_email = $_REQUEST[“email”];
$str_pwd = $_REQUEST[“pwd”];
$sql = “insert into users(email, password) values(‘”.$str_email.”‘, ‘”.$str_pwd.”‘)”;

//insert data to database
if(!mysql_query($sql,$con)) {
return false;
}
else {
return true;
}

//close connection
mysql_close($con);

}

?>

 

Mainfest.xml

———————————–

<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.databasetest_1″
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”15″ />
<uses-permission android:name=”android.permission.INTERNET”/>

<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.MainActivity”
android:label=”@string/title_activity_main” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

 

 

Very Clear [ Android <=> Mysql ], no explain more, type again then u also can find out!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s