• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting Variable From PHP file to Android's MainActivity

Aqsa_Meer

Lurker
Hey I am working on a project in which i am making a verification system (Phone_Number) like whats app.
I am generating a random number in php file i want to take this number in android's main_activity.
How can i do it ?
This is my php file


<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'verification');
define('DB_USER','root');
define('DB_PASSWORD','');

// 1. Create a database connection
$connection = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD);
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
echo"Server Connected";
// 2. Select a database to use
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}
echo "Db Connected";
{
$PhoneNum=$_POST['PhoneNumber'];
$Random = rand(100000 , 600000);
print_r($_POST);
echo $Random;
$query="INSERT INTO `phonebook`(`PhoneNumber`, `RandomNumber`) VALUES ($PhoneNum, $Random)";
$query_exec = mysqli_query($connection ,$query) ;
}?>


MainActivity,java





package com.example.verificationdatabase;
import android.view.View.OnClickListener;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText etPhoneNumber;
EditText RandomNumber;
Button Submit;
Button response;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//StricTPolicy
StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
etPhoneNumber=(EditText) findViewById(R.id.editText1);
RandomNumber= (EditText) findViewById(R.id.editText2);
//Setting up the id for the button
Submit= (Button) findViewById(R.id.button1);
//Setting Up the On click Listener
Submit.setOnClickListener(new View.OnClickListener()
{
InputStream is=null;

@override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Storing the values inside the EditText inside the String Variable
String PhoneNumber=""+etPhoneNumber.getText().toString();
Log.i("Tag","Phone number: "+PhoneNumber);
// System.out.println("testing");
//Setting Up the nameValuePairs
List<NameValuePair> nameValuePairs =new ArrayList<NameValuePair>(1);
//Adding the string variable inside the NameValuePair
nameValuePairs.add(new BasicNameValuePair("PhoneNumber", PhoneNumber));

//Setting Up the Connection inside the try catch block
try{
//Setting up the default http client
HttpClient httpClient= new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://192.168.1.5/older/index.php");
// System.out.println("Inside try");
//passing the name pair variables inside the httpPost
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Log.i("Tag","http post:" +httpPost);
//Log.i("Tag","value:" +nameValuePairs);
//System.out.println("para"+nameValuePairs);



//Getting the response
HttpResponse response =httpClient.execute(httpPost);
Log.i("Tag","UR;"+ response);
//setting up the http response
HttpEntity entity =response.getEntity();
Log.i("Tag","UR;"+ entity);
//Setting up the content inside an input stream reader
//Lets define the input stream reader
is = entity.getContent();
//Displaying the Toast msg if the data is entered successfully
String msg="Data is entered successfully" +PhoneNumber;
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
catch(ClientProtocolException e)
{
Log.e("ClientProtocol","Log_tag");
e.printStackTrace();
System.out.println("Excep: "+e);

}
catch(IOException e)
{
Log.e("Log_tag","IOException");
e.printStackTrace();

}
}
});

////////////////////
OnClickListener stopListener = new OnClickListener() {
public void onClick(View v) {
InputStream is=null;
List<NameValuePair> nameValuePairs =new ArrayList<NameValuePair>(1);
String RandomString=""+RandomNumber.getText().toString();
Toast.makeText(getApplicationContext(), "You entered"+ RandomString, Toast.LENGTH_LONG).show();
try{
//Setting up the default http client
HttpClient httpClient= new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://192.168.1.5/older/check.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Getting the response
HttpResponse response =httpClient.execute(httpPost);
Log.i("Tag","UR;"+ response);
//setting up the http response
HttpEntity entity =response.getEntity();
Log.i("Tag","UR;"+ entity);
//Setting up the content inside an input stream reader
//Lets define the input stream reader
is = entity.getContent();
String msg="Yupiee" +RandomNumber;
//Displaying the Toast msg if the data is entered successfully
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
catch(ClientProtocolException e)
{
Log.e("ClientProtocol","Log_tag");
e.printStackTrace();
System.out.println("Excep: "+e);

}
catch(IOException e)
{
Log.e("Log_tag","IOException");
e.printStackTrace();

}
} };
response = (Button)findViewById(R.id.button2);
response.setOnClickListener(stopListener);
// db se extract , compare random number

}

@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, 7so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
 
Back
Top Bottom