我是Android开发的新手。我正在创建一个应用程序,用户需要从应用程序本身注册自己。我得到的问题是在插入一次后我无法再次插入记录。换句话说,如果我从我的设备插入一次,则无法插入记录。我需要等待至少15分钟才能插入另一条记录。我正在使用PHP PDO在mysql数据库中插入记录。
这是PHP代码:
<?php
$nm = $_GET['nm'];
$email = $_GET['email'];
$password = $_GET['pass'];
$con = new PDO("mysql:host=hostname; dbname=database", "username", "passwoprd");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$query = $con->prepare("INSERT INTO Entrepreneurs(entFullName, entEmailId, entPassword)
VALUES(:name, :email, :passWord)");
$query->bindParam(':name', $nm);
$query->bindParam(':email', $email);
$query->bindParam(':passWord', $password);
$query->execute();
}catch(PDOException $ex) {
echo "Some Exception Occured: <br />" . $ex->getMessage();
}
?>
这里是java代码:
package com.example.entrepreneurexpress.entrepreneurs;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.entrepreneurexpress.R;
public class EntrepreneurRegister extends Activity {
String extracted_email, extracted_password, extracted_fullName;
EditText YourName, email, password;
ProgressDialog pDialog;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entrepreneur_register);
ActionBar aBar = getActionBar();
aBar.setDisplayHomeAsUpEnabled(true);
Button btnEntClear = (Button) findViewById(R.id.btnEntRegClear);
Button btnEntRegister = (Button) findViewById(R.id.btnEntRegRegister);
YourName = (EditText) findViewById(R.id.txtEntRegFullName);
email = (EditText) findViewById(R.id.txtEntRegEmailId);
password = (EditText) findViewById(R.id.txtEntRegPassword);
btnEntClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (YourName.length() >= 1) {
YourName.setText("");
}
if (email.length() >= 1) {
email.setText("");
}
if (password.length() >= 1) {
password.setText("");
}
}
});
btnEntRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(YourName.length() < 1 || email.length() < 1 || password.length() < 1) {
Toast.makeText(getApplicationContext(), "Please Fill In All The Details", Toast.LENGTH_LONG).show();
} else {
new RegisterTask().execute("register");
}
}
});
}
class RegisterTask extends AsyncTask<String,Void,String>{
protected void onPreExecute(){
pDialog = new ProgressDialog(EntrepreneurRegister.this);
pDialog.setTitle("Processing..");
pDialog.setMessage("Registering Yourself With Us.......");
pDialog.setCancelable(true);
pDialog.setIndeterminate(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params){
// TODO Auto-generated method stub
extracted_email = email.getText().toString();
extracted_fullName = YourName.getText().toString();
extracted_password = password.getText().toString();
String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + extracted_fullName +
"&email=" + extracted_email + "&pass=" + extracted_password;
try {
HttpClient client = new DefaultHttpClient();
URI website;
website = new URI(requesturl);
HttpGet request = new HttpGet();
request.setURI(website);
client.execute(request);
} catch (IOException e) {
e.printStackTrace();
} catch(URISyntaxException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if(pDialog != null) {
pDialog.dismiss();
}
Toast.makeText(getApplicationContext(), "Sucessfully Inserted !", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), WelcomeEntrepreneur.class);
startActivity(i);
}
}
}
更新
我开始知道错误是什么:
当我在android应用程序中使用FirstName LastName
这样的空格键入名称时,它不会被插入但是当我在输入名称FirstNameLastName
时不插入任何空格时,它会被插入。
现在的问题是:我如何解决上述错误?
请帮我解决这个问题。感谢。
答案 0 :(得分:1)
您需要对您在URL字符串中传递的输入进行URL编码。改变这个
String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + extracted_fullName +
"&email=" + extracted_email + "&pass=" + extracted_password;
类似
String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + URLEncoder.encode(extracted_fullName= +
"&email=" + URLEncoder.encode(extracted_email) + "&pass=" + URLEncoder.encode(extracted_password);
还可以考虑将HTTP动词从GET更改为例如POST。对于修改某些内容的请求,GET在语义上是错误的。
答案 1 :(得分:0)
使用POSTMan工具在localhost中检查此PHP-PDO文件输出。 试试这个链接。PHP - PDO
更改php文件代码
<?php
$nm = $_POST['nm'];
$email = $_POST['email'];
$password = $_POST['pass'];
$con = new PDO("mysql:host=hostname; dbname=database", "username", "passwoprd");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$query = $con->prepare("INSERT INTO Entrepreneurs(entFullName, entEmailId, entPassword)
VALUES(:name, :email, :passWord)");
$query->bindParam(':name', $nm);
$query->bindParam(':email', $email);
$query->bindParam(':passWord', $password);
$query->execute();
}catch(PDOException $ex) {
echo "Some Exception Occured: <br />" . $ex->getMessage();
}
?>
在java代码中,您使用了requesturl ...更改它..
String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=extracted_fullName&email=extracted_email&pass=extracted_password"
试试这个