我正在制作一个Android应用程序,我需要登录和注册,我是编程的初学者,我做了一些没有显示任何结果或错误的工作。 `
package com.example.user.appliication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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 org.json.JSONObject;
import java.util.HashMap;
public class RegisterActivity extends AppCompatActivity {
EditText ETname, ETusername, ETpassword, ETconfpas, ETrollnumber, ETemail;
Button RegisterButton;
StringRequest request;
RequestQueue queue;
String URL = "http://user.000webhostapp.com/folder/script.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ETname = (EditText) findViewById(R.id.etName);
ETusername = (EditText) findViewById(R.id.etUsername);
ETpassword = (EditText) findViewById(R.id.etPassword);
ETconfpas = (EditText) findViewById(R.id.etConPass);
ETrollnumber = (EditText) findViewById(R.id.etRollnumber);
ETemail = (EditText) findViewById(R.id.etEmail);
queue = Volley.newRequestQueue(this);
RegisterButton = (Button) findViewById(R.id.regButton);
RegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ETname.getText().toString().matches("")||ETusername.getText().toString().matches("")||ETpassword.getText().toString().matches("")||ETconfpas.getText().toString().matches("")||ETrollnumber.getText().toString().matches("")||ETemail.getText().toString().matches("")){
Toast.makeText(RegisterActivity.this, "All fields required", Toast.LENGTH_SHORT).show();
}
else{
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
boolean success = jsonObject.getBoolean("success");
if(success){
String message = jsonObject.getString("message");
Intent i = new Intent(RegisterActivity.this, HomeActivity.class);
startActivity(i);
Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show();
}
else {
String message = jsonObject.getString("message");
Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(RegisterActivity.this, volleyError.toString(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected HashMap<String, String> getParams(){
HashMap<String,String> params = new HashMap<String, String>();
params.put("name",ETname.getText().toString());
params.put("username",ETusername.getText().toString());
params.put("password",ETpassword.getText().toString());
params.put("rollnumber",ETrollnumber.getText().toString());
params.put("email",ETemail.getText().toString());
return params;
}
};
queue.add(request);
}
}
});
}
} `
用于注册的PHP脚本是
<?php
$response = array();
if (isset($_POST['user_name']) && isset($_POST['user_password']) &&
isset($_POST['user_fullname'])) {
// include db connect
require_once __DIR__ . '/connection.php';
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];
$rollnumber = $_POST['rollnumber'];
$email = $_POST['email'];
$query = "INSERT INTO appusers(name ,username, password, rollnumber, email)
VALUES ('$name','$username','$password','$rollnumber','$email')";
// insert into db
$result = mysqli_query($con,$query);
//check if registration completed
if ($result) {
$response["success"] = true;
$response["message"] = "Registration completed";
// echo JSON response
echo json_encode($response);
}
else{
$response["success"] = false;
$response["message"] = "Rollnumber alreaday exist";
// echo JSON response
echo json_encode($response);
}
}
else{
$response["success"] = false;
$response["message"] = "Something went wrong";
// echo JSON response
echo json_encode($response);
}
?>
当我运行此应用程序时,ADB停止而没有显示任何错误响应,也没有将数据发送到我的数据库。
答案 0 :(得分:0)
我从未使用过StringRequest你是否尝试过不使用AsyncTask将它放在主线程上? 它看起来像这样:
在按钮中点击:
//Trust me you need a progress bar
pb = (ProgressBar) this.findViewById(R.id.regProgressBar);
RegisterButton = (Button) findViewById(R.id.regButton);
RegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginLoader ll = new LoginLoader(pb, RegisterActivity.this);
String[] args = new String[6];
args[0] = URL;
args[1] = ETname.getText().toString();
args[2] = ETusername.getText().toString();
args[3] = ETemail.getText().toString();
args[4] = ETpassword.getText().toString();
args[5] = ETrollnumber.getText().toString();
ll.execute(args);
});
public class LoginLoader extends AsyncTask<String, Void, String> {
private final ProgressBar _pb;
private final Activity _act;
public LoginLoader(ProgressBar pb, Activity act)
{
_pb = pb;
_act = act;
}
@Override
protected String doInBackground(String... params) {
try {
String url = params[0];
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setDoInput(true);
con.setRequestMethod("POST");
if(params.length > 1) {
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write("name=" + URLEncoder.encode(params[1], "UTF-8") + "&");
writer.write("username=" + URLEncoder.encode(params[2], "UTF-8") + "&");
writer.write("email=" + URLEncoder.encode(params[3], "UTF-8") + "&");
writer.write("password=" + URLEncoder.encode(params[4], "UTF-8") + "&");
writer.write("rollnumber=" + URLEncoder.encode(params[5], "UTF-8") + "&");
writer.close();
}
InputStreamReader reader = new InputStreamReader(con.getInputStream());
char[] buf = new char[1024];
StringBuilder sb = new StringBuilder();
int len = 0;
while((len = reader.read(buf)) >0)
{
sb.append(buf, 0, len);
}
reader.close();
return sb.toString();
}
catch (Exception ex)
{
return null;
}
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
boolean success = jsonObject.getBoolean("success");
if(success){
String message = jsonObject.getString("message");
Intent i = new Intent(_act, HomeActivity.class);
_act.startActivity(i);
Toast.makeText(_act, message, Toast.LENGTH_SHORT).show();
}
else {
String message = jsonObject.getString("message");
Toast.makeText(_act, message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
_pb.setVisibility(ProgressBar.INVISIBLE);
}
@Override
protected void onPreExecute() {
_pb.setVisibility(ProgressBar.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {}
}