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

Claphands

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.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Error org.json.JSONException: Value 1 at error of type java.lang.Integer cannot be converted to boolean

From Dev

org.json.JSONException: Value <div of type java.lang.String cannot be converted to JSONObject during the debugging

From Dev

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

From Dev

Value Bundle of type java.lang.String cannot be converted to JSONArray

From Dev

How to solve error: Value UPDATE of type java.lang.String cannot be converted to JSONObject

From Dev

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

From Dev

Error parsing data org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject

From Dev

Value <br of type java.lang.String cannot be converted to JSONObject on android

From Dev

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

From Dev

java.lang.String cannot be converted to JSONObject

From Dev

JSONException: Value 'cityId':4,cityName:'Ahmedabad 'java.lang.String cannot be converted to JSONObject

From Dev

org.json.JSONException: Value [....] of type org.json.JSONArray cannot be converted to JSONObject

From Dev

org.json.JSONException: Value 0 at success of type java.lang.String cannot be converted to JSONArray

From Dev

Error org.json.JSONException: Value 1 at error of type java.lang.Integer cannot be converted to boolean

From Dev

org.jsonexception value of type org.jsonobject cannot be converted to jsonarray

From Dev

error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

From Dev

Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject

From Dev

org.json.JSONException: Value of Feedback type java.lang.String cannot be converted to JSONArray while converting jsonstring to object

From Dev

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

From Dev

Value Bundle of type java.lang.String cannot be converted to JSONArray

From Dev

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

From Dev

ERROR: com.android.volley.ParseError: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

From Dev

java.lang.String cannot be converted to JSONObject. mismatch type

From Dev

Get Org.json.JSONException:Value array(2) of type java.lang.String can not be converted to JSONObject

From Dev

org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject in Android Studio with phpserver

From Dev

Value Connection of type java.lang.String cannot be converted to JSONObject in getting objects from the database

From Dev

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

From Dev

org.json.JSONException:Value[{}] of type org.json.JSONArray cannot be converted to JSONObject

Related Related

  1. 1

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

  2. 2

    Error org.json.JSONException: Value 1 at error of type java.lang.Integer cannot be converted to boolean

  3. 3

    org.json.JSONException: Value <div of type java.lang.String cannot be converted to JSONObject during the debugging

  4. 4

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

  5. 5

    Value Bundle of type java.lang.String cannot be converted to JSONArray

  6. 6

    How to solve error: Value UPDATE of type java.lang.String cannot be converted to JSONObject

  7. 7

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

  8. 8

    Error parsing data org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject

  9. 9

    Value <br of type java.lang.String cannot be converted to JSONObject on android

  10. 10

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

  11. 11

    java.lang.String cannot be converted to JSONObject

  12. 12

    JSONException: Value 'cityId':4,cityName:'Ahmedabad 'java.lang.String cannot be converted to JSONObject

  13. 13

    org.json.JSONException: Value [....] of type org.json.JSONArray cannot be converted to JSONObject

  14. 14

    org.json.JSONException: Value 0 at success of type java.lang.String cannot be converted to JSONArray

  15. 15

    Error org.json.JSONException: Value 1 at error of type java.lang.Integer cannot be converted to boolean

  16. 16

    org.jsonexception value of type org.jsonobject cannot be converted to jsonarray

  17. 17

    error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

  18. 18

    Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject

  19. 19

    org.json.JSONException: Value of Feedback type java.lang.String cannot be converted to JSONArray while converting jsonstring to object

  20. 20

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

  21. 21

    Value Bundle of type java.lang.String cannot be converted to JSONArray

  22. 22

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

  23. 23

    ERROR: com.android.volley.ParseError: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

  24. 24

    java.lang.String cannot be converted to JSONObject. mismatch type

  25. 25

    Get Org.json.JSONException:Value array(2) of type java.lang.String can not be converted to JSONObject

  26. 26

    org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject in Android Studio with phpserver

  27. 27

    Value Connection of type java.lang.String cannot be converted to JSONObject in getting objects from the database

  28. 28

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

  29. 29

    org.json.JSONException:Value[{}] of type org.json.JSONArray cannot be converted to JSONObject

HotTag

Archive