dpl22899 2018-10-23 21:05
浏览 141
已采纳

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

  • 写回答

1条回答

  • douchong4730 2018-10-23 23:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题