public class activityresult2 extends Activity {
public static String TAG = activityresult2.class.getSimpleName();
static public String txtOrder = "";
private int sum =5;
TextView foodordershow,costtext;
String orders,cost;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_activityresult2);
foodordershow = (TextView) findViewById(R.id.foodordershow);
costtext = (TextView) findViewById(R.id.costtext);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orders =foodordershow.getText().toString();
cost = costtext.getText().toString();
}
});
//display all the orders
Bundle bundle = getIntent().getExtras();
String strfnq = bundle.getString("Noodle quantity");
String strfrq = bundle.getString("Rice quantity");
String strfsq = bundle.getString("Fish quantity");
String stricq = bundle.getString("Iced tea");
Integer strsum = bundle.getInt("sum");
boolean addNingc = bundle.getBoolean("ANI");
boolean addRingc = bundle.getBoolean("ARI");
boolean addFingc = bundle.getBoolean("AFI");
boolean addTingc = bundle.getBoolean("AIT");
boolean addmoneyc = bundle.getBoolean("AMY");
//calculate the checkbox values
Log.d(TAG, "BOOLEAN VALUES" + addNingc + addRingc + addFingc + addTingc + addmoneyc);
Log.d(TAG, "String VALUES" + strfnq + strfrq + strfsq + strfsq + strfsq);
Log.d(TAG, "SUM VALUES" + sum);
Intent mIntent = getIntent();
TextView foodorders = (TextView) findViewById(R.id.foodordershow);
foodorders.setText(getIntent().getExtras().getString("Quantity"));
String addNdlThing = "";
int tSum=0;
//if each checkbox is checked , state that addition of ingredients
if (addNingc) {
addNdlThing = " with addition of ingredients and ";
tSum+=sum;
}
String addRlThing = "";
if (addRingc) {
addRlThing = " with addition of ingredient and ";
tSum+=sum;
}
String addSlThing = "";
if (addFingc) {
addSlThing = " with addition of ingredients and ";
tSum+=sum;
}
String addTeac = "";
if (addTingc) {
tSum+=sum;
addTeac = " with addition of ingredients and ";
}
//display the checkbox values and add it to the values from quantity
Log.d(TAG, "SUM VALUES AFTER " +tSum);
int totalSum = mIntent.getIntExtra("sum", strsum) + tSum;
TextView costtext = (TextView) findViewById(R.id.costtext);
costtext.setText(String.valueOf(totalSum));
//how the orders are displayed
foodorders = (TextView) findViewById(R.id.foodordershow);
if (strfnq.equals("") && strfrq.equals("") && strfsq.equals("") && stricq.equals("")) {
txtOrder = "Sorry, You've not ordered any thing , please return to previous menu to order";
} else if (!strfnq.equals("") && !strfrq.equals("") && !strfsq.equals("") && stricq.equals("")) {
txtOrder = "Thank you , You've ordered\n" + strfnq + " fried noodle" + addNdlThing + " and\n" + strfrq
+ " fried rice" + addRlThing + " and\n" + strfsq + " Steam fish " + addSlThing + "and\n" + stricq + " Steam fish " + addTeac;
} else {
txtOrder = "Thank you , You've ordered\n";
if (!strfnq.equals("")) {
txtOrder = txtOrder + strfnq + " fried noodle" + addNdlThing;
}
if (!strfrq.equals("")) {
txtOrder = txtOrder + strfrq + " fried rice" + addRlThing;
}
if (!strfsq.equals("")) {
txtOrder = txtOrder + strfsq + " Steam fish" + addSlThing;
}
if (!stricq.equals("")) {
txtOrder = txtOrder + stricq + " Iced Tea" + addTeac;
}
}
foodorders.setText(txtOrder);
}
这是我的php文件
class DBOperations{
private $host = '127.0.0.1';
private $user = 'root';
private $db = 'learn2crack-login-register';
private $pass = '';
private $conn;
public function __construct() {
$this -> conn = new PDO("mysql:host=".$this -> host.";dbname=".$this -> db,$this -> user, $this -> pass);
}
public function insertData($name,$email,$password,$orders){
$unique_id = uniqid('', true);
$hash = $this->getHash($password);
$encrypted_password = $hash["encrypted"];
$salt = $hash["salt"];
$sql = 'INSERT INTO users SET unique_id =:unique_id,name =:name,
email =:email,orders =:orders,encrypted_password =:encrypted_password,salt =:salt,created_at = NOW()';
$query = $this ->conn ->prepare($sql);
$query->execute(array('unique_id' => $unique_id, ':name' => $name, ':orders' => $orders, ':email' => $email,
':encrypted_password' => $encrypted_password, ':salt' => $salt));
if ($query) {
return true;
} else {
return false;
}
}
public function checkLogin($email, $password) {
$sql = 'SELECT * FROM users WHERE email = :email';
$query = $this -> conn -> prepare($sql);
$query -> execute(array(':email' => $email));
$data = $query -> fetchObject();
$salt = $data -> salt;
$db_encrypted_password = $data -> encrypted_password;
if ($this -> verifyHash($password.$salt,$db_encrypted_password) ) {
$user["name"] = $data -> name;
$user["email"] = $data -> email;
$user["unique_id"] = $data -> unique_id;
return $user;
} else {
return false;
}
}
public function changePassword($email, $password){
$hash = $this -> getHash($password);
$encrypted_password = $hash["encrypted"];
$salt = $hash["salt"];
$sql = 'UPDATE users SET encrypted_password = :encrypted_password, salt = :salt WHERE email = :email';
$query = $this -> conn -> prepare($sql);
$query -> execute(array(':email' => $email, ':encrypted_password' => $encrypted_password, ':salt' => $salt));
if ($query) {
return true;
} else {
return false;
}
}
public function checkUserExist($email){
$sql = 'SELECT COUNT(*) from users WHERE email =:email';
$query = $this -> conn -> prepare($sql);
$query -> execute(array('email' => $email));
if($query){
$row_count = $query -> fetchColumn();
if ($row_count == 0){
return false;
} else {
return true;
}
} else {
return false;
}
}
public function getHash($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = password_hash($password.$salt, PASSWORD_DEFAULT);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
public function verifyHash($password, $hash) {
return password_verify ($password, $hash);
}
}
我想将这两个 TextView 订单和费用保存到我的sql数据库中,请帮助我谢谢,我正在使用wampserver与3 php文件将它们连接到我的wampserver。因为复制和粘贴php文件是不可能的,因为有太多的代码。是否有人愿意给我一步一步的指导,就像我首先要在我的数据库中创建一个包含成本和订单的表.. < em>我如何编码,以便我可以将文本发送到wampserver
答案 0 :(得分:0)
使用Asynktask保存您的数据,如下所示
class SaveData extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(YourActivity.this, AlertDialog.THEME_HOLO_LIGHT);
pDialog.setMessage("Please wait..");
pDialog.setCancelable(true);
pDialog.setIndeterminate(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
String data = URLEncoder.encode("ordersParameterName", "UTF-8")
+ "=" + URLEncoder.encode(orders, "UTF-8");
data += "&" + URLEncoder.encode("costParameterName", "UTF-8")
+ "=" + URLEncoder.encode(cost, "UTF-8");
URL url = new URL("your url here");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pDialog.cancel();
//your success to insert value
}
}
您可以点击提交按钮
来调用它new SaveData().execute();