org.json.JSONException:值为10的java.lang.String类型的电子邮件无法转换为JSONObject

i have a signup form which takes the username,email,password and mobile using json php and store them in php..but at runtime it is iving followin error.

org.json.JSONException: Value Email Already Exist at 0 of type java.lang.String cannot be converted to JSONObject W/System.err:
at org.json.JSON.typeMismatch(JSON.java:100)

my sinup.java is:

   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;

    }

}

database:enter image description here

dongxiz5342
dongxiz5342 价值电子邮件已经存在于0的java.lang.String类型无法转换为JSONObject,你可以..转储php响应?我不是很确定,但似乎你的android部分没有阅读任何其他json除了{“注册”:{“msg”:{“status”:“xxx”}}}..但是只要你尝试就会发生错误读错了json。PS。我也不认为截图或UI部分与您的问题相关..
接近 2 年之前 回复

1个回答



您应该按照在java代码中使用它的方式将响应包装在多维数组中。</ p>
\ n

现在你基本上将json数组的 signup </ code>部分设置为</ p>

“Email Already Exist”</ code > </ p>

这是无效的json解码。</ p>

这应该可以解决这个问题:php部分中的 </ code> < / p>

  if(check_customer_email($ con,$ email)){
$ msg [“status”] = 0;
$ msg [“msg”] =“电子邮件已经 存在“;
$ response [”注册“] [] = $ msg;
}
else {
$ sql =”插入客户(cus_name,cust_email,cust_userpass,cus_mobile)值('$ name',' $ email','$ userpass','$ phone')“;
if(mysqli_query($ con,$ sql)){
$ msg [”status“] = 1;
$ msg [”msg“] =“值已成功添加”;
$ response [“注册”] [] = $ msg;
}
else {
$ msg [“status”] = 0;
$ response [“注册”] [ ] = $ msg;
}
</ code> </ pre>

罪魁祸首特别是这个州 ent [和代码中的那些] </ p>

  array_push($ response [“Signup”],$ msg [“status”]); 
</ code> </ pre>

$ msg ['status'] </ code>将是一个普通的字符串,您希望通过不仅仅将一个键[数组中的“status”</ code>]。</ p>
</ div>

展开原文

原文

You should wrap your response inside a multidemnsional array in the way you are using it in the java code.

Right now you have basicly set the signup part of the json array to the value of

"Email Already Exist"

which is not valid json to be decoded.

This should do the trick: 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;
}

The culprit in particular is this statement [and the ones like it in the code]

array_push($response["Signup"], $msg["status"]);

$msg['status'] will be a plain string where you would want to make this value an array by not just assingning it one key ["status"] from the array.

doumao1887
doumao1887 msg [status]的值为“email already exists”,不会转换为可以再次解码的jsonobject。
接近 2 年之前 回复
duanhuai7532
duanhuai7532 我刚刚做了更改,因为你告诉我现在正在工作......感谢先生...但我还有一个困惑,我的相同代码正在为店主方面工作但不在这里......我也不明白你这条线的含义 “$ msg ['status']将是一个普通的字符串,你可以通过不仅仅从数组中确定一个键[”status“]来使这个值成为一个数组。
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐