gpt4 book ai didi

php - 从 PHP 接收 json 回显

转载 作者:行者123 更新时间:2023-11-30 01:32:50 24 4
gpt4 key购买 nike

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

我们将不胜感激。

我的 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);

肯定有最简单的方法来做到这一点。

谢谢!

最佳答案

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

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');
}

关于php - 从 PHP 接收 json 回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35523203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com