volley.parserror:org.json.JSONException:java.lang.string类型的值br无法转换为JSONObject
volley.parserror:org.json 无法将java.lang.string类型的值br转换为JSONObject 安卓代码volley.parserror:org.json.JSONException:java.lang.string类型的值br无法转换为JSONObject,java,php,android,android-studio,Java,Php,Android,Android Studio,volley.parserror:org.json 无法将java.lang.string类型的值br转换为JSONObject 安卓代码 public void performSearch() { String url= "http://192.168.0.136/fyp/stitle.php"; RequestQueue requestQueue = Volley.newRequestQueue(Stitle.this);
public void performSearch() {
String url= "http://192.168.0.136/fyp/stitle.php";
RequestQueue requestQueue = Volley.newRequestQueue(Stitle.this);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("Response", response.toString());
try {
//converting the string to json array object
JSONObject array = new JSONObject();
Log.i("test", " value : " + array.getString("status"));
if (array.getString("status").equals("true")) {
JSONArray jsonArray = array.getJSONArray("search");
Log.i("test", " value : " + array);
for (int i = 0; i < jsonArray.length(); i++) {
//getting product object from json array
JSONObject product = jsonArray.getJSONObject(i);
//adding the product to product list
boolean add = productList.add(new list(
product.getLong("isbn"),
product.getString("title"),
product.getString("authors"),
product.getInt("accession"),
product.getString("publisher"),
product.getInt("pubyear"),
product.getInt("pages"),
product.getInt("rak"),
product.getInt("hr"),
product.getInt("vr"),
product.getLong("barcode")
));
}
} else {
Log.i("test", "else error");
}
} catch (JSONException e) {
e.printStackTrace();
Log.i("test", e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "error:" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Title", searchtitle.getText().toString());
return params;
}
};
requestQueue = Volley.newRequestQueue(Stitle.this);
requestQueue.add(jsObjRequest);
}
}
public void performSearch(){
字符串url=”http://192.168.0.136/fyp/stitle.php";
RequestQueue RequestQueue=Volley.newRequestQueue(Stitle.this);
JsonObjectRequest jsObjRequest=新的JsonObjectRequest(Request.Method.POST,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.i(“Response”,Response.toString());
试一试{
//将字符串转换为json数组对象
JSONObject数组=新的JSONObject();
Log.i(“测试”,“值:”+array.getString(“状态”));
if(array.getString(“status”).equals(“true”)){
JSONArray JSONArray=array.getJSONArray(“搜索”);
Log.i(“测试”,“值:”+数组);
for(int i=0;i
将json发送到android java文件的Php代码文件
您的代码有几个问题。首先,让我们来处理您的PHP。您的PHP代码应该设计为返回一些有用的信息,无论它是成功的还是失败的——以及失败的原因
我不知道您使用哪个类访问MySQL数据,但无论如何它都不是PDO
。我将向您展示一个使用PDO
的示例,因为您可以使用准备好的语句,这将有助于保护您免受SQL注入攻击。
您的PHP代码设计糟糕,此外,它还容易受到SQL注入攻击
在PHP代码开始时,先对参数进行isset
检查。无论查询是否失败,此代码都将返回一个JSONObject
。这使得我们更容易发现什么地方出了问题——什么时候出了问题
<?php
// array for JSON response
$response = array();
//set values just in case any thing goes wrong
$response["status"] = 0;
$response["message"] = "Error before start";
// check for post data with isset
if (isset($_POST["Title"])) {
$title = $_POST["Title"];
// You were not using PDO so I dumped your connection and require you to provide...
//...a configuration file for ...
require_once __DIR__ . '/db_config.php';
// ...these variables
$host = DB_SERVER;
$db = DB_DATABASE;
$user = DB_USER;
$pass = DB_PASSWORD;
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try{
// connecting to db with PDO
$pdo = new PDO($dsn, $user, $pass, $opt);
$sql = 'SELECT isbn, title, authors, accession, publisher, pubyear, pages, rak, hr, vr, barcode
FROM books
WHERE title LIKE :titleParam';
$titleParam = "%".$title."%";
$stmt = $pdo->prepare($sql);
// Bind the parameter
$stmt->bindParam(':titleParam', $titleParam, PDO::PARAM_STR);
$res = $stmt->execute();
if ($res) {
// success
$response["status"] = 1;
// connection node
$response["books"] = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$data = array();
$data["isbn"] = $row["isbn"];
$data["title"] = $row["title"];
$data["authors"] = $row["authors"];
$data["accession"] = $row["accession"];
$data["publisher"] = $row["publisher"];
$data["pubyear"] = $row["pubyear"];
$data["pages"] = $row["pages"];
$data["rak"] = $row["rak"];
$data["hr"] = $row["hr"];
$data["vr"] = $row["vr"];
$data["barcode"] = $row["barcode"];
array_push($response["books"], $data);
}
}
else {
// required field is missing
$response["status"] = 2;
$response["message"] = "No data returned";
}
}
catch (Exception $e){
$response["status"] = 3;
$response["message"] = "Error occurred." . $e->getMessage();
}
}
else {
$response["status"] = 4;
$response["message"] = "Post parameters are not correct";
}
// echoing JSON response
echo json_encode($response);
?>
将结果记录到logcat中,以便查看返回的JSON响应,然后发布数据示例。您应该被告知PHP代码已打开,可以进行SQL注入!您还应该使用isset
检查POST参数,因此如果参数设置不正确,PHP代码将优雅地退出。logcat上没有JSON响应。屏幕上出现错误@你的代码有几个问题。一个问题涵盖的内容太多。但首先,您的PHP并不是为了返回JSONArray
!它被设计为返回一个JSONObject
。其次,您的PHP代码设计糟糕,而且容易受到SQL注入攻击!在任何时候使用变量筛选结果集时,都应该使用准备好的语句。您应该使用isset
作为POST参数。当PHP代码因任何原因失败时,应该返回某种格式良好的JSON响应。修复后,我们就可以开始使用Android代码了。。现在我将代码更改为JSONObject,将php更改为isset。但是同样的错误出现了。。我在编码帮助方面很差,这出现在logcat>org.json.JSONException上:status没有值,这是因为您的代码JSONObject array=new JSONObject();Log.i(“测试”,“值:”+array.getString(“状态”))代码>::您正在创建一个新对象数组
,但其中没有任何内容!::您需要编写以下代码:Log.i(“测试”,“值:”+response.optInt(“状态”,-1))
I/test:value:4 I/test:org.json.JSONException:status没有值看起来状态值是“4”,根据PHP代码,这意味着参数没有正确发送。现在我如何正确发送参数
<?php
// array for JSON response
$response = array();
//set values just in case any thing goes wrong
$response["status"] = 0;
$response["message"] = "Error before start";
// check for post data with isset
if (isset($_POST["Title"])) {
$title = $_POST["Title"];
// You were not using PDO so I dumped your connection and require you to provide...
//...a configuration file for ...
require_once __DIR__ . '/db_config.php';
// ...these variables
$host = DB_SERVER;
$db = DB_DATABASE;
$user = DB_USER;
$pass = DB_PASSWORD;
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try{
// connecting to db with PDO
$pdo = new PDO($dsn, $user, $pass, $opt);
$sql = 'SELECT isbn, title, authors, accession, publisher, pubyear, pages, rak, hr, vr, barcode
FROM books
WHERE title LIKE :titleParam';
$titleParam = "%".$title."%";
$stmt = $pdo->prepare($sql);
// Bind the parameter
$stmt->bindParam(':titleParam', $titleParam, PDO::PARAM_STR);
$res = $stmt->execute();
if ($res) {
// success
$response["status"] = 1;
// connection node
$response["books"] = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$data = array();
$data["isbn"] = $row["isbn"];
$data["title"] = $row["title"];
$data["authors"] = $row["authors"];
$data["accession"] = $row["accession"];
$data["publisher"] = $row["publisher"];
$data["pubyear"] = $row["pubyear"];
$data["pages"] = $row["pages"];
$data["rak"] = $row["rak"];
$data["hr"] = $row["hr"];
$data["vr"] = $row["vr"];
$data["barcode"] = $row["barcode"];
array_push($response["books"], $data);
}
}
else {
// required field is missing
$response["status"] = 2;
$response["message"] = "No data returned";
}
}
catch (Exception $e){
$response["status"] = 3;
$response["message"] = "Error occurred." . $e->getMessage();
}
}
else {
$response["status"] = 4;
$response["message"] = "Post parameters are not correct";
}
// echoing JSON response
echo json_encode($response);
?>