我正在尝试创建登录信息&使用Android Studio注册我的应用。我可以很好地完成我的代码而没有任何错误,但是由于某种原因它没有将寄存器详细信息保存到我的数据库中。
我使用www.one.com作为我的数据库主机。
注册脚本(php)
<?php
$con=mysqli_connect("
myHost","myUser","mypass","myDatabaseName"); //Removed for safety purposes.
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO User (name, age, username, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
获取用户数据文件(php)
<?php
$con=mysqli_connect("
myHost","myUser","mypass","myDatabaseName"); //Removed for safety purposes.
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user["name"] = $name;
$user["age"] = $age;
$user["username"] = $username;
$user["password"] = $password;
}
echo json_encode($user);
mysqli_close($con);
?>
服务器请求
public class ServerRequest {
ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADDRESS = "http://www.domain.be/";
public ServerRequest(Context context) {
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing...");
progressDialog.setMessage("Please wait...");
}
public void storeUserDataInBackground(User user,
GetUserCallback userCallBack) {
progressDialog.show();
new StoreUserDataAsyncTask(user, userCallBack).execute();
}
public void fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
progressDialog.show();
new fetchUserDataAsyncTask(user, userCallBack).execute();
}
/**
* parameter sent to task upon execution progress published during
* background computation result of the background computation
*/
public class StoreUserDataAsyncTask extends AsyncTask<Void, Void, Void> {
User user;
GetUserCallback userCallBack;
public StoreUserDataAsyncTask(User user, GetUserCallback userCallBack) {
this.user = user;
this.userCallBack = userCallBack;
}
@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
dataToSend.add(new BasicNameValuePair("age", user.age + ""));
HttpParams httpRequestParams = getHttpRequestParams();
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS
+ "Register.php");
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private HttpParams getHttpRequestParams() {
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
return httpRequestParams;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
userCallBack.done(null);
}
}
public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallback userCallBack;
public fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
this.user = user;
this.userCallBack = userCallBack;
}
@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS
+ "FetchUserData.php");
User returnedUser = null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject.length() != 0){
Log.v("happened", "2");
String name = jObject.getString("name");
int age = jObject.getInt("age");
returnedUser = new User(name, age, user.username,
user.password);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser) {
super.onPostExecute(returnedUser);
progressDialog.dismiss();
userCallBack.done(returnedUser);
}
}
}
注册
public class Register extends AppCompatActivity implements View.OnClickListener {
EditText etName, etAge, etUsername, etPassword;
Button bRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etName = (EditText) findViewById(R.id.etName);
etAge = (EditText) findViewById(R.id.etAge);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
bRegister = (Button) findViewById(R.id.bRegister);
bRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bRegister:
String name = etName.getText().toString();
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
int age = Integer.parseInt(etAge.getText().toString());
User user = new User(name, age, username, password);
registerUser(user);
break;
}
}
private void registerUser(User user) {
ServerRequest serverRequest = new ServerRequest(this);
serverRequest.storeUserDataInBackground(user, new GetUserCallback() {
@Override
public void done(User returnedUser) {
Intent loginIntent = new Intent(Register.this, Login.class);
startActivity(loginIntent);
}
});
}
}
答案 0 :(得分:0)
解决了!我做了两件事来完成这项工作;
我使用免费托管我的数据库。我认为我的付费托管拒绝服务器上的php脚本。
我在build.gradle文件中更改了一些内容。
apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "AppID" minSdkVersion 18 targetSdkVersion 21 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:21.0.3' }
我还下载了Android v21并更改了应用中的一些较新元素以使用旧版本。