我还是Android编程的新手,目前正在学习如何连接到DB。 我正在进行在线教程,但我仍然遇到问题。 本教程是关于创建注册表单并通过齐射将其发送到数据库。 我使用.hostinger.ae作为主机和Android工作室。 一切似乎没问题,直到我点击注册按钮它给我一个错误(无法注册) 我无法找到错误,调试显示没有错误。所以,如果您有任何帮助,我将不胜感激。
我有两个php文件上传到公共文件夹上的在线主机
dbconnect php用于连接数据库的文件
\w
和volleyRegister php
<?php
define('HOST','mysql.hostinger.ae');
define('USER','--');
define('PASS','--');
define('DB','u853541076_tuber');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
我的主要活动.java http://tuber.16mb.com是我的域名
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
require_once('dbConnect.php');
$sql = "INSERT INTO volley (username,password,email) VALUES('$username','$email','$password')";
if(mysqli_query($con,$sql)){
echo "Successfully Registered";
}else{
echo "Could not register";
}
}else{
echo 'error';
}
manifest.java
package myapp.com.tuberapp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
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 org.json.JSONException;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
///The hosting link
public static final String REGISTER_URL = "http://tuber.16mb.com/volleyRegister.php";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_EMAIL = "email";
private EditText editTextUsername;
private EditText editTextEmail;
private EditText editTextPassword;
private Button buttonRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = (EditText) findViewById(R.id.editTextUsername);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextEmail= (EditText) findViewById(R.id.editTextEmail);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
buttonRegister.setOnClickListener(this);
}
private void registerUser(){
final String username = editTextUsername.getText().toString().trim();
final String password = editTextPassword.getText().toString().trim();
final String email = editTextEmail.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.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);
}
@Override
public void onClick(View v) {
if(v == buttonRegister){
registerUser();
}
}
}