当我尝试使用凌空,PHP和mysql在android studio中创建注册时遇到了一些麻烦,但是我还是新手,所以我无法解决该问题。我签入了logcat,但没有任何错误。当我在设备上使用时,收到消息错误“ 注册错误org.json.JSONException:值类型java.lang.String的电子邮件无法转换为JSONObject ”
package com.labs.penjualan.project.penjualan;
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.ProgressBar;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
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;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button button_to_login,button_to_sign_up;
private EditText nama,email,password;
private ProgressBar loading;
private static String URL_REGIST = "http://192.168.43.22/penjualan/register.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.button_to_login);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openLoginActivity();
}
});
loading = findViewById(R.id.loading); //Register
nama = findViewById(R.id.nama);
email= findViewById(R.id.email);
password = findViewById(R.id.password);
button_to_sign_up = findViewById(R.id.button_to_sign_up);
button_to_sign_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Register();
}
});
}
private void Register(){
loading.setVisibility(View.VISIBLE);
button_to_sign_up.setVisibility(View.GONE);
final String nama = this.nama.getText().toString().trim();
final String email = this.email.getText().toString().trim();
final String password = this.password.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGIST, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success"); // I think this is the trouble
if (success.equals("1")){
Toast.makeText(MainActivity.this, "Register Success!" , Toast.LENGTH_SHORT ).show();
}
}catch(JSONException e){
e.printStackTrace();
Toast.makeText(MainActivity.this, "Register Error " + e.toString() , Toast.LENGTH_SHORT ).show();
loading.setVisibility(View.GONE);
button_to_sign_up.setVisibility(View.VISIBLE);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Register Error" + error.toString() , Toast.LENGTH_SHORT ).show();
loading.setVisibility(View.GONE);
button_to_sign_up.setVisibility(View.VISIBLE);
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("nama", nama);
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
public void openLoginActivity(){
Intent intent = new Intent (this, LoginActivity.class);
startActivity(intent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="105dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/background_gradient" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="150dp"
android:id="@+id/layoutInput"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_centerHorizontal="true"
android:background="@drawable/background_white"
android:padding="20dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Full name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/nama"
android:drawablePadding="20dp"
android:drawableRight="@drawable/ic_person"
android:hint="Your Name"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="5dp"
android:text="Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/email"
android:drawablePadding="20dp"
android:drawableRight="@drawable/ic_person"
android:hint="Your Email"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="5dp"
android:text="password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/password"
android:drawablePadding="20dp"
android:drawableRight="@drawable/ic_person"
android:hint="******"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_to_sign_up"
android:textColor="@color/colorblueinstagram"
android:background="@drawable/rec_gradient"
android:layout_marginTop="20dp"
android:textAllCaps="false"
android:text="Sign Up"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="5dp"
android:text="or"
android:textSize="17dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_to_login"
android:layout_marginTop="10dp"
android:backgroundTint="@color/colorblueinstagram"
android:textColor="#FFF"
android:background="@drawable/rec_gradient_fb"
android:textAllCaps="false"
android:text="Sign In"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/loading"
android:layout_marginTop="5dp"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="15dp" />
</LinearLayout>
<ImageView
android:id="@+id/imageVIew"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:background="@drawable/background_white"
android:padding="5dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:textSize="20sp"
android:textColor="#FFF"
android:layout_centerHorizontal="true"
android:layout_below="@id/imageVIew"
android:text="Sign Up"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<?php
$conn = new mysqli('localhost', 'root', '', 'penjualan');
if($_SERVER['REQUEST_METHOD'] == 'POST' ){
$nama = $_POST['nama'];
$email = $_POST['email'];
$password = $_POST['password'];
$uuid = sha1("$email");
$password = password_hash($password, PASSWORD_DEFAULT);
$cek = "SELECT * from user where email = '$email' ";
$result = $conn->query($cek);
if ($result->num_rows == 0){
$sql = "INSERT INTO `user` (`uuid`, `nama`, `email`, `password`) VALUES ('$uuid', '$nama', '$email', '$password')";
if ($conn->query($sql) === TRUE) {
echo "1";
echo "Success";
} else {
echo "0 ";
echo "Error ";
}
}else{
echo "Email Telah Terdaftar";
}
}
?>