简体   繁体   中英

JSONException: Value of type java.lang.String cannot be converted to JSONObject

I'm new to android, but I found an app that would go great with a web scraper that I've been working very hard on and filled a database with. This is a basic app that retrieves values from a MySQL database using a PHP script and displays them on your android device.

Here is the PHP:

<?php
try {
    $conn = new PDO('mysql:host=localhost;dbname=beanbag', **********, *******);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $results=$conn->query('SELECT * FROM Recipes');

    while ($results = $row->fetch(PDO::FETCH_ASSOC)){
    $output[]=$row;
    print(json_encode($output));
    }

} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
$conn = null;
?>

The original code used mysql_connect but I implemented PDO instead.

Here is the Java:

protected void onPostExecute(Void v) {

        // ambil data dari Json database
        try {
            JSONArray Jarray = new JSONArray(result);
            for(int i=0;i<Jarray.length();i++)
            {
            JSONObject Jasonobject = null;
            //text_1 = (TextView)findViewById(R.id.txt1);
            Jasonobject = Jarray.getJSONObject(i);

            //get an output on the screen
            //String id = Jasonobject.getString("id");
            String name = Jasonobject.getString("Title");
            String db_detail="";

            if(et.getText().toString().equalsIgnoreCase(name)) {
            db_detail = Jasonobject.getString("ingredient");
            text.setText(db_detail);
            break;
            }
            //text_1.append(id+"\t\t"+name+"\t\t"+password+"\t\t"+"\n");

            }
            this.progressDialog.dismiss();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }

Logcat says:

Error parsing data org.json.JSONException: Value br of type java.lang.String cannot be converted to JSONArray

I tried using {left brace and }right brace solution from this stackoverflow question , but I don't think that's it.

On post execute get JSONObject first then array. If still same message then your server is not returning correct json object.

JSONObject jobj = new JSONObject(result);
JSONArray Jarray = jobj.getJSONArray("output");
<?php
try {
    $conn = new PDO('mysql:host=localhost;dbname=beanbag', **********, ********);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

$sql=mysql_query("select * from Recipes");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

This doesn'tdo anything. You're mixing up mysql_* and PDO which is not possible . If you make a connection with PDO, you can't use mysql_* queries to access the database.

Because of this, you get errors and your page is not valid Json, which causes the error.

Instead, use only PDO (mysql_* is deprecated due to security issues):

<?php
try {
    $conn = new PDO('mysql:host=localhost;dbname=beanbag', **********, ********);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

$sql = $conn->query("select * from Recipes");
while (($row = $sql->fetch(PDO::FETCH_ASSOC)) !== false)
    $output[]=$row;
print(json_encode($output));
?>

If your result is array list then You can do this using following code

ArrayList<String> list = new ArrayList<String>();
list.add("blah");
list.add("bleh");
JSONArray jsArray = new JSONArray(list);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM