Я работаю над приложением для входа и регистрации.
Моя проблема заключается в том, что сообщение об ошибке показывает, есть ли ошибка или регистрация прошла успешно. Так что я не знаю, где ошибка.
КОД:
package test.test.com.test;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.content.Intent;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Registraition extends ActionBarActivity {
Button b_login;
Button b_register;
RadioGroup gender;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
EditText first_name,last_name,email,pass,cpass;
String birth,gender_value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registraition);
b_login = (Button)findViewById(R.id.button_go_login);
b_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Registraition.this, MainActivity.class));
setContentView(R.layout.activity_main);
}
});
b_register = (Button)findViewById(R.id.button_register);
b_register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(Registraition.this, "",
"Register user...", true);
new Thread(new Runnable() {
public void run() {
register();
}
}).start();
}
});
}
void register () {
try{
first_name = (EditText)findViewById(R.id.editText_firstname);
last_name = (EditText)findViewById(R.id.editText_lastname);
pass = (EditText)findViewById(R.id.editText_password);
cpass = (EditText)findViewById(R.id.editText_cpassword);
email = (EditText)findViewById(R.id.editText_email);
birth = DatePickerFragment.date;
gender = (RadioGroup)findViewById(R.id.gender);
int id = gender.getCheckedRadioButtonId();
if (id == R.id.radioButton_female){
gender_value = "female";
} else {
gender_value = "male";
}
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://192.168.178.174/app/register.php");
nameValuePairs = new ArrayList<NameValuePair>(7);
nameValuePairs.add(new BasicNameValuePair("firstname",first_name.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("lastname",last_name.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("email",email.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("passwordconfirm",cpass.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("birth",birth.trim()));
nameValuePairs.add(new BasicNameValuePair("gender",gender_value.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("Success")){
//Here the Mistacke begins
Toast.makeText(Registraition.this,"Register Success", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Registraition.this, MainActivity.class));
setContentView(R.layout.activity_main);
}else {
showAlert();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public void showAlert(){
Registraition.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Registraition.this);
builder.setTitle("Registraition Error.");
builder.setMessage("Error on Registraition")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
Это вывод:
07-26 14:58:52.326 1902-2286/test.test.com.test I/System.out﹕ Response : ERROR
07-26 14:58:52.447 1902-1929/test.test.com.test W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-26 14:58:52.447 1902-1929/test.test.com.test W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5ba7520, error=EGL_SUCCESS
Но Пользователь был зарегистрирован. Код PHP был:
<?php require("../login/common.php");
$message = '';
$signup = "false";
if(!empty($_POST))
{
if(empty($_POST["firstname"]) or empty($_POST["lastname"]) or empty($_POST["password"]) or empty($_POST["passwordconfirm"]) or empty($_POST["birth"])) {
die("ERROR");
}
elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
die("ERROR");
}
elseif($_POST["password"] == $_POST["passwordconfirm"]) {
$password = test_input($_POST["password"]);
$cpassword = test_input($_POST["passwordconfirm"]);
if (strlen($_POST["password"]) <= '5') {
die("Password to short(min. 6)");
}
}
elseif(!empty($_POST["password"])) {
die("ERROR");
}
$signup = "true";
if($signup == "true") {
$query = "SELECT
1
FROM users
WHERE
email = :email
";
$query_params = array(
':email' => $_POST['email']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
//normaly die("ERROR") but this is one of the mistackes
}
$row = $stmt->fetch();
if($row)
{
}
$query = "INSERT INTO users (
username,
password,
salt,
email,
gender,
birthday
) VALUES (
:username,
:password,
:salt,
:email,
:gender,
:birthday
)
";
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$password = hash('sha256', $_POST['password'] . $salt);
for($round = 0; $round < 65536; $round++)
{
$password = hash('sha256', $password . $salt);
}
$gender = $_POST['gender'];
$birthday = $_POST['birth'];
$username = $_POST['firstname'] . " " . $_POST['lastname'];
$query_params = array(
':username' => $username,
':password' => $password,
':salt' => $salt,
':email' => $_POST['email'],
':gender' => $gender,
':birthday' => $birthday
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
//normaly die("ERROR") but this is one of the mistackes
}
if($signup == "true") {
$result = mysql_query("SELECT * FROM users");
$num_rows = mysql_num_rows($result);
mkdir("C:/xampp/htdocs/Users/" . $_POST['email']);
$src = "C:/xampp/htdocs/Users/Sample";
$dst = "C:/xampp/htdocs/Users/" . $_POST['email'];
xcopy($src,$dst);
$savemodi = $_POST["savemodi"];
if($savemodi == true) {
$saveinfopath = "C:/xampp/htdocs/Users/" . $_POST['email'] . "/";
$file = fopen($saveinfopath . $savemodi . "/infos.txt","w");
$userinfos = fwrite($file,$username . "°" . $_POST['email'] . "°" . $gender . "°" . $birthday . "°" . $result['id']);
fclose($file);
$saveinfopath = "C:/xampp/htdocs/Users/" . $_POST['email'] . "/";
$file = fopen($saveinfopath . "public/infos.txt","w");
$userinfos = fwrite($file,$username . "°°°°" . $result['id']);
fclose($file);
} else {
$saveinfopath = "C:/xampp/htdocs/Users/" . $_POST['email'] . "/";
$file = fopen($saveinfopath . $savemodi . "/infos.txt","w");
$userinfos = fwrite($file,$username . "°" . $_POST['email'] . "°" . $gender . "°" . $birthday . "°" . $result['id']);
fclose($file);
$saveinfopath = "C:/xampp/htdocs/Users/" . $_POST['email'] . "/";
$file = fopen($saveinfopath . "private/infos.txt","w");
$userinfos = fwrite($file,$username . "°" . $_POST['email'] . "°" . $gender . "°" . $birthday . "°" . $result['id']);
fclose($file);
}
die("Success");
} else {
die("ERROR");
}?>
Поэтому я отмечаю ОШИБКИ в коде PHP комментарием.
Других решений пока нет …