作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理一个问题。我有我的 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/
我是一名优秀的程序员,十分优秀!