我有一个注册表单,该表单使用json php接收用户名,电子邮件,密码和移动设备,并将其存储在php ..中,但是在运行时却显示了Followin错误。
org.json.JSONException:值电子邮件已经存在,类型为0 无法将java.lang.String转换为JSONObject W / System.err:
在org.json.JSON.typeMismatch(JSON.java:100)
我的sinup.java是:
package com.breera.automechanic_storeapp;
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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Signup extends AppCompatActivity {
private RequestQueue mQueue;
EditText cusname,cusemail,cuspass,cusmobile,confrmpass;
String cus_name,cus_email,cus_pass,cus_cnfrmPass,cus_mobile;
Button btnlogin,btnSignup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
cusname=findViewById(R.id.fn);
cusemail=findViewById(R.id.cusemail);
cuspass=findViewById(R.id.cuspass);
cusmobile=findViewById(R.id.cusmobile);
confrmpass=findViewById(R.id.confrmpass);
btnlogin=(Button)findViewById(R.id.logbtn);
btnSignup=findViewById(R.id.signup);
mQueue = Volley.newRequestQueue(this);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intnt=new Intent(Signup.this,CusLogin.class);
startActivity(intnt);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(Signup.this, "hhh", Toast.LENGTH_SHORT).show();
onsignup();
}
});
}public void onsignup(){
cus_name = cusname.getText().toString();
cus_email = cusemail.getText().toString();
cus_pass = cuspass.getText().toString();
cus_cnfrmPass=confrmpass.getText().toString();
cus_mobile = cusmobile.getText().toString();
try {
UrlPassClasa urlPassClasa=new UrlPassClasa("customer/signup.php?name=" + cus_name +"&email=" + cus_email+ "&userpass=" + cus_pass+ "&cus_mobile=" + cus_mobile);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlPassClasa.getUrl(), null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Signup");
JSONObject signup = jsonArray.getJSONObject(0);
int signupStatus = signup.getInt("status");
// String signupmsg = signup.getString("msg");
if (signupStatus == 1) {
Toast.makeText(Signup.this, "han", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Signup.this, "Failed", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
Toast.makeText(Signup.this, "Error", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}//1st try
catch (Exception e) {
Toast.makeText(Signup.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}//end mthd
}
activity_signup.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bck"
tools:context=".Signup">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rider's Signup"
android:textSize="36sp"
android:layout_marginTop="5dp"
android:textColor="#fff"
android:gravity="center"
android:textStyle="bold"
android:fontFamily="cursive"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bck"
android:layout_marginTop="0dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="15dp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:layout_marginRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#fff"
android:elevation="4dp"
android:padding="10dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_fn"
android:layout_width="match_parent"
android:layout_marginTop="0dp"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/fn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_people_black_24dp"
android:hint="Name"
android:inputType="text"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_email"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/cusemail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_email_black_24dp"
android:singleLine="true"
android:hint="Mail Address"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/cuspass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_lock_black_24dp"
android:drawableTint="@color/colorPrimaryDark"
android:singleLine="true"
android:hint="Password"
android:inputType="numberPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/confrmpass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:singleLine="true"
android:drawableLeft="@drawable/ic_lock_open_black_24dp"
android:hint=" Confirm Password"
android:inputType="numberPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/cusmobile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:singleLine="true"
android:drawableLeft="@drawable/ic_phone_android_black_24dp"
android:hint=" Mobile No"
android:inputType="numberPassword" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/logbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:textAllCaps="false"
android:layout_gravity="right"
android:textColor="@color/colorPrimaryDark"
android:text="Already an account.Login" />
<Button
android:id="@+id/signup"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_margin="5dp"
android:background="@drawable/bck"
android:text="Sign up"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
signup.php:
<?php
require_once("../includes/db.php");
require_once("../includes/functions.php");
$response["Signup"] = array();
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$userpass = $_REQUEST["userpass"];
$phone = $_REQUEST["cus_mobile"];
//$name = "zain";
//$email = "zain@gmail.com";
//$userpass = "123";
//$phone = "123456";
$msg = array();
if (check_customer_email($con, $email)) {
$msg["status"] = 0;
$msg["msg"] = "Email Already Exist";
array_push($response["Signup"], $msg["msg"]);
}
else {
$sql = "insert into customer(cus_name, cust_email, cust_userpass,cus_mobile) values ('$name', '$email', '$userpass','$phone')";
if(mysqli_query($con, $sql)) {
$msg["status"] = 1;
$msg["msg"] = "Values successfully Added";
array_push($response["Signup"], $msg["msg"]);
}
else {
$msg["status"] = 0;
array_push($response["Signup"], $msg["status"]);
}
}
echo json_encode($response);
?>
UrlPassClasa.java:
package com.breera.automechanic_storeapp;
public class UrlPassClasa {
String Url="http://192.168.0.107/auto_mechanic/";
public UrlPassClasa(String file) {
Url=Url+file;
}
public String getUrl(){
return Url;
}
}
答案 0 :(得分:0)
您应该按照在Java代码中使用响应的方式,将响应包装在多维数组中。
现在,您已经基本将json数组的signup
部分设置为
"Email Already Exist"
这不是要解码的有效json。
这应该可以解决问题:in the php part
if (check_customer_email($con, $email)) {
$msg["status"] = 0;
$msg["msg"] = "Email Already Exist";
$response["Signup"][] = $msg;
}
else {
$sql = "insert into customer(cus_name, cust_email, cust_userpass,cus_mobile) values ('$name', '$email', '$userpass','$phone')";
if(mysqli_query($con, $sql)) {
$msg["status"] = 1;
$msg["msg"] = "Values successfully Added";
$response["Signup"][] = $msg;
}
else {
$msg["status"] = 0;
$response["Signup"][] = $msg;
}
罪魁祸首尤其是此语句[以及代码中类似的语句]
array_push($response["Signup"], $msg["status"]);
$msg['status']
将是一个纯字符串,您可以在其中通过将其值不只是从数组中分配一个键["status"
]来使它成为数组。