gpt4 book ai didi

java - volley.parsererror :org. json.JSONException : value br of type java. lang.string 无法转换为 JSONObject

转载 作者:行者123 更新时间:2023-12-02 11:11:57 28 4
gpt4 key购买 nike

volley.parsererror:org.json

value br of type java.lang.string cannot be converted to JSONObject

Android 代码

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

}

}

Php Code file to send json to android java file

    <?php
include"connection.php";

if (isset($title = $_POST["Title"]){

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$query =$conn->prepare('SELECT isbn, title, authors, accession, publisher, pubyear, pages, rak, hr, vr, barcode FROM books where title like "%'.$title.'%" ');

$query->execute();

$query->bind_result($isbn, $title, $authors, $accession, $publisher, $pubyear, $pages, $rak, $hr, $vr, $barcode);


$books = array();
$data =array();
//traversing through all the result
while($query->fetch()){
$temp = array();
$temp['isbn'] = $isbn;
$temp['title'] = $title;
$temp['authors'] = $authors;
$temp['accession'] = $accession;
$temp['publisher'] = $publisher;
$temp['pubyear'] = $pubyear;
$temp['pages'] = $pages;
$temp['rak'] = $rak;
$temp['hr'] = $hr;
$temp['vr'] = $vr;
$temp['barcode'] = $barcode;

array_push($data, $temp);

}
$books['status'] = true;
$books['search'] = $data;



//displaying the result in json format
echo json_encode($books);
}}
?>

When run applicaion this error toast on screen [volley.parsererror:org.json.JSONException: value br of type java.lang.string cannot be converted to JSONArray]

最佳答案

您的代码存在几个问题。首先让我们处理您的 PHP。您的 PHP 代码应该设计为返回一些有用的信息,无论它是成功还是失败 - 以及失败的原因。

我无法判断您正在使用哪个类来访问 MySQL 数据,但无论如何它都不是 PDO。我将向您展示一个使用 PDO 的示例,因为您可以使用准备好的语句,这将有助于保护您免受 SQL 注入(inject)攻击。你的 PHP 代码设计得很糟糕,而且很容易受到 SQL 注入(inject)攻击!

在 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);
?>

关于java - volley.parsererror :org. json.JSONException : value br of type java. lang.string 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50556738/

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