0

I read some about the error in google , i am bit new to php

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

This is my php

$sqlchk="SELECT STATUS FROM OBJECTS WHERE ID=$id";
$res = mysqli_query($con,$sqlchk);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('ID'=>$row[0],
'STATUS'=>$row[1]
));
}


echo json_encode(array("result"=>$result));

this is my code java

try {
    try {
        responses = client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String jsonData = responses.body().string();
    JSONObject Jobject = new JSONObject(jsonData);
    JSONArray Jarray = Jobject.getJSONArray("result");

    if (!responses.isSuccessful()) throw new IOException("Unexpected code " + responses);


    for (int i = 0; i < Jarray.length(); i++) {
        JSONObject object     = Jarray.getJSONObject(i);
        String    prize = object.getString("STATUS");
        System.out.println(prize);
    }
} catch (JSONException e) {
        Log.e("MYAPP", "unexpected JSON exception", e);
}
    catch (Exception e) {
   Log.e("MYAPP", "unexpected io exception", e);
}

edit 1

$id= isset($_POST['id']) ? intval($_POST['id']) : null;
$likes= isset($_POST['likes']) ? intval($_POST['likes']) : null;


$conn = mysqli_connect($host,$uname,$pwd,$db) or die(mysqli_error());

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

$sql = "UPDATE OBJECTS SET LIKES=$likes where ID=$id ";


if ($conn->query($sql) === TRUE) {

} 

$sqlchk="SELECT ID,STATUS FROM OBJECTS WHERE ID=$id";
$res = mysqli_query($conn,$sqlchk);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('ID'=>$row[0],
'STATUS'=>$row[1]
));
}


echo json_encode(array("result"=>$result));



$conn->close();
23
  • 1
    You should post your JSON data Commented Sep 17, 2016 at 22:07
  • 2
    Why don't you debug your app, place a breakpoint on line JSONObject Jobject = new JSONObject(jsonData) and verify what your input string contains? I suspect PHP displays some sort of warning or notice. Commented Sep 17, 2016 at 22:08
  • The problem is actually inside the for loop Commented Sep 17, 2016 at 22:09
  • if (!responses.isSuccessful()) should be before you attempt anything with the response data Commented Sep 17, 2016 at 22:10
  • 1
    forget about the android app for now. fix the php script first. once you have the correct JSON by accessing your url thru your web browser, you can now check from your android app. Commented Sep 18, 2016 at 12:16

1 Answer 1

1

You need to fix the PHP code first and make sure that it gives you the right JSON string. To verify, try to manually access the php file with the proper POST headers (id and likes).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.