当我尝试运行我的代码以通过我的远程托管数据库上的POST方法注册用户时,我在我的应用程序中混合了2个错误。第一个是:
排球服务器错误
第二个是一个非常长的混乱,基本上总结为:
此网站需要使用javascript才能运行。请在您的浏览器中启用javascript。
我很困惑,因为据我所知,我的代码中没有使用javascript。如果有人能帮我解决这个问题,我将永远感激不尽。
register.php
<?php
require_once 'include/constants.php';
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$stmt = mysqli_prepare($con, "INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $email, md5($password));
$stmt->execute();
$stmt->close();
和我的
Register.class
package com.thinkgeist.stashbox;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
/**
* Created by natha on 2016-03-10.
*/
public class Register extends AppCompatActivity implements View.OnClickListener {
Button bRegister;
EditText etUsername, etEmail, etPassword, etConfirm;
PopupWindow popUp;
LinearLayout mainLayout;
public static final String KEY_USERNAME = "username";
public static final String KEY_EMAIL = "email";
public static final String KEY_PASSWORD = "password";
public static final String REGISTER_URL = "http://sb.thinkgeist.com/register.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
popUp = new PopupWindow(this);
mainLayout = new LinearLayout(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
etConfirm = (EditText) findViewById(R.id.etConfirm);
etEmail = (EditText) findViewById(R.id.etEmail);
bRegister = (Button) findViewById(R.id.bRegister);
bRegister.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.bRegister:
// Responds to register button being clicked
String username = etUsername.getText().toString();
String email = etEmail.getText().toString();
String password = etPassword.getText().toString();
String passConfirm = etConfirm.getText().toString();
if(password.length() == 0 || email.length() == 0 || username.length() == 0 || passConfirm.length() == 0){
Intent popUp = new Intent(this, Pop.class);
popUp.putExtra("error_message", "All fields must be filled in to register!");
startActivity(popUp);
break;
}
// Test if email is of proper format
if(!email.contains("@")){
Intent popUp = new Intent(this, Pop.class);
popUp.putExtra("error_message", "Email is of wrong format!");
startActivity(popUp);
break;
}
// Test if password length is okay
if(password.length() <= 5 || password.length() > 20){
Intent popUp = new Intent(this, Pop.class);
popUp.putExtra("error_message", "Password must be between 6-20 characters!");
startActivity(popUp);
break;
}
if(username.length() <= 5 || username.length() >= 20){
Intent popUp = new Intent(this, Pop.class);
popUp.putExtra("error_message", "Username must be between 6-20 characters!");
startActivity(popUp);
break;
}
if(!password.contentEquals(passConfirm)){
Intent popUp = new Intent(this, Pop.class);
popUp.putExtra("error_message", "Passwords do not match!");
startActivity(popUp);
break;
}
User registeredData = new User(username, email, password);
Intent popUp = new Intent(this, Pop.class);
registerNewUser(this, registeredData);
popUp.putExtra("error_message", "Account sucessfully created!");
//startActivity(popUp);
break;
}
}
public void registerNewUser(Context context, User toRegister){
final String username = toRegister.username;
final String password = toRegister.password;
final String email = toRegister.email;
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(Register.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Register.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,username);
params.put(KEY_PASSWORD,password);
params.put(KEY_EMAIL, email);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
对于这篇长篇文章感到抱歉,但我觉得在没有提供足够信息的情况下提出问题几乎是多余的。感谢任何抽出时间帮助的人!