从 PHP 接收 json 回显


Receiving json echo from PHP

我在这里处理一个问题。我有我的php代码,它向我发送了一个包含所有信息的用户数组。问题是:我如何从安卓访问所有这些信息?(不是以丑陋的方式,我要在这里发布)

任何帮助将不胜感激。

我的 php 文件

<?php
$host = "mysql12.000webhost.com";
$user = "xxxx";
$password = "xxx";
$db = "xxxx";
$con = mysqli_connect($host,$user,$password,$db);

    $username = $_POST["username"];
    $password = $_POST["password"];
    $statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $username, $password);
    mysqli_stmt_execute($statement);
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $name, $age, $mail, $username, $password,$image);
    $user = array();
    while(mysqli_stmt_fetch($statement)){
        $user[name] = $name;
        $user[age] = $age;
        $user[mail] = $mail;
        $user[username] = $username;
        $user[password] = $password;
        $user[image] = $image;
    }    
    echo json_encode($user);
    mysqli_close($con);
?> 

我处理信息的方式:

StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) { //Read till there is something available
                sb.append(line + "");//Reading and saving line by line - not all at once
            }
        line = sb.toString();
        String[] kvPairs = line.split(",");
        String[] nombreIn = kvPairs[0].split(":");
        String nombre_com = nombreIn[1].replaceAll(" '"", "");
        String nombre = nombre_com.replace("'"", "");
        ///TAQUIBIEEEEEEEN!!!

        String[] ageIn = kvPairs[1].split(":");
        int age = Integer.parseInt(ageIn[1]);
        String[] mailIn = kvPairs[2].split(":");
        String mail = mailIn[1].replaceAll("'"", "");
        String[] usernameIn = kvPairs[3].split(":");
        String username = usernameIn[1].replaceAll("'"", "");

        String[] passwordIn = kvPairs[4].split(":");
        String password = passwordIn[1].replaceAll("'"", "");
        String[] bitmstrIn = kvPairs[5].split(":");
        String[] bitmstrIn2 = bitmstrIn[1].split("''}");
        String bitStr = bitmstrIn2[0].replaceAll("'n", "");;
        String biStrFin = bitStr.replaceAll("''''", "");
        String bitkok=biStrFin.substring(0, biStrFin.length() - 1);

当然,必须有一种最简单的方法可以做到这一点。

谢谢!

这是您从 JSON 中检索信息的方式

StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) { //Read till there is something available
    sb.append(line + "");//Reading and saving line by line - not all at once
}
line = sb.toString();
JSONObject user = null;
try {
   user = new JSONObject(line);
} catch (JSONException e) {
   //code in case variable line isnt well format json string
}
if (user != null) {
   String name = user.getString('name');
   String age = user.getString('age');
   String mail = user.getString('mail');
   String userName = user.getString('username');
   String password = user.getString('password');
   String image = user.getString('image');
}