JSON异常字符串无法转换为JSONObject出现

时间:2016-10-05 23:15:33

标签: php android json

我正在Android上做一个应用程序,我想与php文件进行通信。我能够通信但我无法获得用户ID并将其传递给Android。当我在Android Studio中输入活动时,JSON Exception字符串无法转换为JSONObject出现在Toast中。请帮我。我尝试了一切。

    private static final String URL = "http://192.168.1.43/Teste/list.php";

        private AbsListView listView;
        private String jsonResult;

        View myView;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            myView = inflater.inflate(R.layout.activity_personal_information,container,false);
            listView = (AbsListView) myView.findViewById(R.id.listView);
            accessWebService();
            return myView;
        }
 .....

        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("Custos");

            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

                String number = jsonChildNode.optString("custos");
                String outPut = number + "€";
                employeeList.add(createEmployee("custos", outPut));
            }
        } catch (JSONException e) {
            Toast.makeText(getActivity(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), employeeList,
                android.R.layout.simple_list_item_1,
                new String[] { "custos" }, new int[] { android.R.id.text1 });
        listView.setAdapter(simpleAdapter);


    }

这是我的php文件,我登录(Login.php)

<?php 

    if($_SERVER['REQUEST_METHOD']=='POST'){
        //Getting values 
        $email = $_POST['email'];
        $password = $_POST['password'];


        $sql = "SELECT * FROM Utilizadores WHERE email='$email' AND password='$password'";

        require_once('connection.php');

        $sql = mysqli_query($con,$sql);


        $check = mysqli_fetch_array($sql,MYSQLI_BOTH);

        //if we got some result 
        if(isset($check)){

            session_start();
            $_SESSION['user_id'] = $check['user_id']; //here is where I save the user ID
            echo "user_id";
        }else{
            //displaying failure
            echo "failure";
        }

这是我的java文件,我获取了login.php

    public static final String LOGIN_URL = "http://192.168.1.43/user/login.php";

    //Keys for email and password as defined in our $_POST['key'] in login.php
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PASSWORD = "password";



    //If server response is equal to this that means login is successful
    public static final String LOGIN_SUCCESS = "user_id";  //*Im using sharedpreferences in Android to mantain session, and I put the user_id here*

    //Keys for Sharedpreferences
    //This would be the name of our shared preferences
    public static final String SHARED_PREF_NAME = "myloginapp";

    //This would be used to store the email of current logged in user
    public static final String EMAIL_SHARED_PREF = "email";


    //We will use this to store the boolean in sharedpreference to track user is loggedin or not
    public static final String LOGGEDIN_SHARED_PREF = "loggedin";
}

    mysqli_close($con);
}

这是我执行查询的php文件(list.php)

<?php
$host="localhost"; 
$username="root"; 
$password=""; 
$db_name="Parking";
session_start();
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "select * from Costs where user_id = ". $_SESSION['user_id']; //Here is where I do the query in order to only have the costs of the logged in user...

$result = mysql_query($sql);
$json = array();

if(mysql_num_rows($result)){
    while($row=mysql_fetch_assoc($result)){
        $json['Costs'][]=$row;
    }
}
mysql_close($con);
echo json_encode($json); 
?>

0 个答案:

没有答案