这里我使用php文件将数据从android存储到远程数据库。 现在在我的android项目中我有注册页面。当我按下注册它将调用php文件并将数据存储到数据库。在我的php文件中,我从数据库中获得两个值,即:
$_SESSION['fullname'] = $row['fullname'];
$_SESSION['vault_no'] = $row['vault_no'];
现在,当按下注册按钮时,我必须在单独的活动中显示这些值(fullname和vault_no)。我需要添加什么额外的代码才能实现这一点..请帮助我..我是android.Thanks的新手..
Register.php
<?php
session_start();
define('HOST','localhost');
define('USER','username');
define('PASS','password');
define('DB','dbname');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
$salutation = ($_POST['salutation']);
$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$dob = trim($_POST['dob']);
$mobile = trim($_POST['mobile']);
$country = trim($_POST['country']);
$state = trim($_POST['state']);
$city = trim($_POST['city']);
$pin = trim($_POST['pin']);
$sql = "SELECT mobile FROM users WHERE mobile = '$mobile' LIMIT 1";
$result = mysqli_query($con,$sql);
if(mysqli_fetch_array($result))
{
$response["error"] = true;
$response["error_msg"] = "user already existed with" . $mobile;
echo json_encode($response);
}
else
{
$characters = '0123456789';
$uuid = '';
$random_string_length = 5;
for ($i = 0; $i < $random_string_length; $i++)
{
$uuid .= $characters[rand(0, strlen($characters) - 1)];
}
$fullname = $fname . " " . $lname;
$vault_no = $salutation . "" . $country . "" . $pin . "" . $uuid;
$sql = "insert into users (salutation,fname,lname,fullname,"
."dob,mobile,country,state,city,pin,unique_id,vault_no,created_at) "
."values ('$salutation','$fname','$lname','$fullname',"
."'$city','$pin','$uuid','$vault_no',now())";
if(mysqli_query($con,$sql))
{
$sql = "SELECT * FROM users WHERE mobile = '$mobile' LIMIT 1";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
$response["error"] = false;
$_SESSION['fullname'] = $row['fullname'];
$_SESSION['vault_no'] = $row['vault_no'];
$response['fullname'] = $row['fullname'];
$response['vault_no'] = $row['vault_no'];
echo json_encode($response);
}
}
else
{
$response["error"] = true;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
mysqli_close($con);
?>
<script>
alert("your message");
document.location = "formprofile.php"
</script>
MainActivity.java (注册页面)
package com.example.server;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings({ "deprecation", "unused" })
public class MainActivity extends Activity
{
// array to save Salutation code
private static final int[] SalutationCode
= new int[]{0,1,2};
// array to show Salutation
private static final String[] Salutation
= new String[]{"Salutation","Mr","Ms"};
// array to save Country code
private static final int[] CountryCode
= new int[]{0,91};
// array to show Country name.
private static final String[] Country
= new String[]{"Country","India"};
// array to save City code
private static final int[] CityCode
= new int[]{0,1};
// array to show City list
private static final String[] City
= new String[]{"City","Bangalore"};
// array to save State code
private static final int[] StateCode
= new int[]{0,1};
// array to show State list
private static final String[] State
= new String[]{"State","Karnataka"};
int selectedAreaCode;
private Spinner spin;
private Spinner spin1;
private Spinner spin2;
private Spinner spin3;
private EditText editTextFname;
private EditText editTextLname;
private EditText editTextDob;
private EditText editTextMobile;
private EditText editTextPin;
private Button register;
//private Button login;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin=(Spinner)
findViewById(R.id.spinner1);
spin1=(Spinner)
findViewById(R.id.spinner2);
spin2=(Spinner)
findViewById(R.id.spinner3);
spin3=(Spinner)
findViewById(R.id.spinner4);
editTextFname=(EditText)
findViewById(R.id.editTextFname);
editTextLname=(EditText)
findViewById(R.id.editTextLname);
editTextDob=(EditText)
findViewById(R.id.editTextDob);
editTextMobile=(EditText)
findViewById(R.id.editTextMobile);
editTextPin=(EditText)
findViewById(R.id.editTextPin);
ArrayAdapter<String> adpt_area
= new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
Salutation);
adpt_area.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adpt_area);
/* ArrayAdapter<CharSequence> spinner1
= ArrayAdapter.createFromResource(this,
R.array.country_arrays,
android.R.layout.simple_spinner_item);
spinner1.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(spinner1);
*/
ArrayAdapter<String> adpt_country
= new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Country);
adpt_country.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(adpt_country);
ArrayAdapter<String> adpt_city
= new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, City);
adpt_city.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adpt_city);
ArrayAdapter<String> adpt_state
= new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, State);
adpt_state.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin3.setAdapter(adpt_state);
}
public void Register(View view)
{
String fname =
editTextFname.getText().toString();
String lname =
editTextLname.getText().toString();
String dob =
editTextDob.getText().toString();
String mobile =
editTextMobile.getText().toString();
String pin =
editTextPin.getText().toString();
int selectedPosition =
spin.getSelectedItemPosition();
// get area code based on value selected in spinner
String salutation =
String.valueOf(SalutationCode[selectedPosition]);
int selectedPosition1 =
spin1.getSelectedItemPosition();
// get area code based on value selected in spinner
String country =
String.valueOf(CountryCode[selectedPosition1]);
int selectedPosition2 =
spin2.getSelectedItemPosition();
// get area code based on value selected in spinner
String state =
String.valueOf(StateCode[selectedPosition2]);
int selectedPosition3 =
spin3.getSelectedItemPosition();
// get area code based on value selected in spinner
String city =
String.valueOf(CityCode[selectedPosition3]);
insertToDatabase(salutation,fname,lname,
dob,mobile,country,state,city,pin);
}
private void insertToDatabase(String salutation,
String fname, String lname,String dob, String mobile,
String country, String state, String city, String pin)
{
class SendPostReqAsyncTask
extends AsyncTask<String, Void, String>
{
@SuppressWarnings({"unused","deprecation"}) @Override
protected String doInBackground(String... params)
{
@SuppressWarnings("unused")
String paramSalutation = params[0];
String paramFname = params[1];
String paramLname = params[2];
String paramDob = params[3];
String paramMobile = params[4];
String paramCountry = params[5];
String paramState = params[6];
String paramCity = params[7];
String paramPin = params[8];
//InputStream is = null;
String fname =
editTextFname.getText().toString();
String lname =
editTextLname.getText().toString();
String dob =
editTextDob.getText().toString();
String mobile =
editTextMobile.getText().toString();
String pin =
editTextPin.getText().toString();
int selectedPosition
= spin.getSelectedItemPosition();
// get area code based on value selected in spinner
String salutation
= String.valueOf(
SalutationCode[selectedPosition]);
int selectedPosition1
= spin1.getSelectedItemPosition();
// get area code based on value selected in spinner
String country
= String.valueOf(
CountryCode[selectedPosition1]);
int selectedPosition2
= spin2.getSelectedItemPosition();
// get area code based on value selected in spinner
String state
= String.valueOf(
StateCode[selectedPosition2]);
int selectedPosition3
= spin3.getSelectedItemPosition();
// get area code based on value selected in spinner
String city
= String.valueOf(
CityCode[selectedPosition3]);
List<NameValuePair> nameValuePairs
= new ArrayList<NameValuePair>();
nameValuePairs.add(new
BasicNameValuePair("salutation", salutation));
nameValuePairs.add(new
BasicNameValuePair("fname", fname));
nameValuePairs.add(new
BasicNameValuePair("lname", lname));
nameValuePairs.add(new
BasicNameValuePair("dob", dob));
nameValuePairs.add(new
BasicNameValuePair("mobile", mobile));
nameValuePairs.add(new
BasicNameValuePair("country", country));
nameValuePairs.add(new
BasicNameValuePair("state", state));
nameValuePairs.add(new
BasicNameValuePair("city", city));
nameValuePairs.add(new
BasicNameValuePair("pin", pin));
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost
= new HttpPost("http://oursite/regsiter.php");
httpPost.setEntity(new
UrlEncodedFormEntity(nameValuePairs));
HttpResponse response
= httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
//is = entity.getContent();
}
catch (ClientProtocolException e)
{ }
catch (IOException e)
{ }
return "success";
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
Toast.makeText(getApplicationContext(),
result, Toast.LENGTH_LONG).show();
//TextView textViewResult = (TextView)
// findViewById(R.id.textViewResult);
//textViewResult.setText("Inserted");
editTextFname.setText("");
editTextLname.setText("");
editTextDob.setText("");
editTextMobile.setText("");
editTextPin.setText("");
}
}
SendPostReqAsyncTask sendPostReqAsyncTask
= new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(salutation,fname,
lname,dob,mobile,country,state,city,pin);
}
@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, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}