Showing posts with label android-application. Show all posts
Showing posts with label android-application. Show all posts

Saturday, 21 December 2013

Connect Android Application With Mysql Database






Register.php

<?php
  $json=$_POST['json'];
  $data=json_decode($json,true);

mysql_connect("localhost","root","");
  mysql_select_db("login");
  mysql_query("insert into register values('".$data['firstname']."','".$data['lastname']."','".$data['gender']."','".$data['username']."','".$data['password']."','".$data['email']."')");
  $output= array('value' => 'data insert sucessfully');
  echo json_encode($output);

?>



Registration.xml


<?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff">
    
        <LinearLayout          
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:orientation="vertical"
            >        
           <LinearLayout          
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="10dp"
            android:orientation="vertical"
            >
        <TextView
        android:id="@+id/fname"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Registration"
        android:textSize="23dp"
        android:layout_marginBottom="5dp"
        android:gravity="center_vertical|center_horizontal"
        android:textColor="#000000"
        />
        </LinearLayout>
        <TextView
        android:id="@+id/fname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Name : "
        android:textSize="17dp"
        android:layout_marginBottom="5dp"
        android:gravity="center_vertical|center_horizontal"
        android:textColor="#000000"
        />
    <EditText
        android:id="@+id/efname"
        android:layout_width="match_parent"
        android:layout_height="40dp"  
        android:textColor="#000000"  
     >
     <requestFocus />
    </EditText>
              
      <TextView
        android:id="@+id/lname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Last Name : "
        android:textSize="17dp"
        android:layout_marginBottom="5dp"
        android:textColor="#000000"
        android:gravity="center_vertical|center_horizontal"
        />
    <EditText
        android:id="@+id/elname"
        android:textColor="#000000"
        android:layout_width="match_parent"
        android:layout_height="40dp"      
     >
    </EditText>    
  
    <TextView
        android:id="@+id/rgender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender : "
        android:textSize="17dp"
        android:layout_marginBottom="5dp"
        android:textColor="#000000"
        android:gravity="center_vertical|center_horizontal"
        />    

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="40dp"  
        android:drawSelectorOnTop="true"
         />

    <TextView
        android:id="@+id/runame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="User Name : "
        android:textSize="17dp"
        android:layout_marginBottom="5dp"
       android:textColor="#000000"
        android:gravity="center_vertical|center_horizontal"
        />
    <EditText
        android:id="@+id/reuname"
        android:textColor="#000000"
        android:layout_width="match_parent"
        android:layout_height="40dp"      
     >
    </EditText>    
  
    <TextView
        android:id="@+id/rpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password : "
        android:textSize="17dp"
        android:layout_marginBottom="5dp"
        android:textColor="#000000"
        android:gravity="center_vertical|center_horizontal"
        />
    <EditText
        android:id="@+id/repass"
        android:layout_width="match_parent"
        android:textColor="#000000"
        android:layout_height="40dp"      
     >
    </EditText>    

   <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email : "
        android:textSize="17dp"
        android:layout_marginBottom="5dp"
        android:textColor="#000000"
        android:gravity="center_vertical|center_horizontal"
        />
    <EditText
        android:id="@+id/eemail"
        android:layout_width="match_parent"
        android:textColor="#000000"
        android:layout_height="40dp"      
     >
    </EditText>    
  


    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp">
      
      <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="Submit"
        android:layout_margin="5dp"
         />


    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="Cancel"
        android:layout_margin="5dp"
        android:layout_toRightOf="@id/submit"/>
  
    </RelativeLayout>
</LinearLayout>
   </ScrollView>




Registration.java


package com.example.project;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

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 org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class register extends Activity implements OnItemSelectedListener{

Spinner mGender;
EditText fname,lname,uname,pass,email;
Button submit;
String returnString;
BufferedReader in = null;
String Gen=null;
protected void onCreate(Bundle savedInstanceState) {
final Context context=this;
super.onCreate(savedInstanceState);
setContentView(R.layout.register);

fname=(EditText)findViewById(R.id.efname);
lname=(EditText)findViewById(R.id.elname);
uname=(EditText)findViewById(R.id.reuname);
pass=(EditText)findViewById(R.id.repass);
email=(EditText)findViewById(R.id.eemail);
submit=(Button)findViewById(R.id.submit);

mGender=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
              android.R.layout.simple_spinner_item, new State[] {
           new State("Male"),
           new State("Female")});
mGender.setAdapter(spinnerArrayAdapter);
   mGender.setOnItemSelectedListener(this);


submit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
try
{
JSONObject json=new JSONObject();
json.put("firstname", fname.getText().toString());
json.put("lastname", lname.getText().toString());
json.put("gender", Gen);
json.put("username", uname.getText().toString());
json.put("password", pass.getText().toString());
json.put("email", email.getText().toString());
postData(json);
Toast.makeText(getBaseContext(), returnString , Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
System.out.println(e);
}
}
});

}
public void postData(JSONObject json)
{
HttpClient httpclient=new DefaultHttpClient();
try
{
HttpPost httppost=new HttpPost("http://10.0.2.2/register.php");
List<NameValuePair> nvp=new ArrayList<NameValuePair>(1);
nvp.add(new BasicNameValuePair("json", json.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nvp));
//HttpResponse response=httpclient.execute(httppost);
HttpResponse response=httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
  String NL = System.getProperty("line.separator");
  while ((line = in.readLine()) != null) {
   sb.append(line + NL);
  }
  in.close();
  String result = sb.toString();

  JSONObject object = new JSONObject(result);
  returnString = object.getString("value");

}
catch(Exception e)
{

}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
  
     State st = (State)mGender.getSelectedItem();
     if ( st != null )
     {
     Gen = st.name;
 
     }
    }
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

}




 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls