volley.parsererror:org.json
value br of type java.lang.string cannot be converted to JSONObject
Android Code
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]
issetand thus give your PHP code an elegant exit if the parameter is not set properly.JSONArray! It is designed to return aJSONObject. Secondly, your PHP code is poorly designed and in addition to that it is open to a SQL injection attack! You should be using a prepared statement any time you are using variables to filter your result set. You should be usingissetfor your POST parameters. You should return some type of well formed JSON response, when your PHP code fails for any reason. After that is fixed, we can get started on the Android code.