-1

I try to build an android app that send a parameter to a php code as a where

query condition in that php code, I should get in log.i the query result but

I get the following error

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

MainActivity

 public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            JSONObject toSend = new JSONObject();
            toSend.put("msg", "3");

            JSONTransmitter transmitter = new JSONTransmitter();
            transmitter.execute(new JSONObject[] {toSend});

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

} 

JSONTransmitter class.

public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> {

    String url = "http://192.168.1.10:89/b.php";


    protected JSONObject doInBackground(JSONObject... data) {
        JSONObject json = data[0];
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
        JSONObject jsonResponse = null;

        HttpPost post = new HttpPost(url);
        try {
            StringEntity se = new StringEntity("json="+json.toString());
            post.addHeader("content-type", "application/x-www-form-urlencoded");
            post.setEntity(se);

            HttpResponse response;
            response = client.execute(post);
            String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());

            jsonResponse=new JSONObject(resFromServer);
            Log.i("Response from server", jsonResponse.getString("msg"));
            Toast.makeText(null, resFromServer, Toast.LENGTH_LONG);
        } catch (Exception e) { e.printStackTrace();}

        return jsonResponse;
    }

}

php code

<?php
 mysql_connect("localhost", "root", "password") 
or die(mysql_error());
mysql_select_db("test") or die(mysql_error());


$result = mysql_query(" select  part_name from  Services_parts where  part_id=  1 ") 
or die(mysql_error());  

$row = mysql_fetch_array( $result );


$j_out = new stdClass();
$j_out->part_name= $row['part_name'];

echo json_encode($j_out);
?>
JEREEF
  • 51
  • 8
  • 4
    Possible duplicate of [JSONException: Value of type java.lang.String cannot be converted to JSONObject](http://stackoverflow.com/questions/10267910/jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-jsonobject) – Chirag Savsani Dec 16 '15 at 11:48

1 Answers1

0

You execute code which is equivalent to this:

JSONObject obj = new JSONObject("john");

what obviously results in exception

this means your json data from server is in wrong format. The problem is probably inside your php code, for returning array of items in json adapt below php+java code:

$res_items = array();
for ( ... )  {
    array_push($res_items, $some_item);
}
$js_result = get_json_encode(array(
                "results" => $res_items
            ));
echo $js_result;

then in java code:

JSONObject jsResp = new JSONObject(serverJsonResult);
JSONArray results = jsResp.getJSONArray("results");
for (int n = 0; n < results.length(); ++n) {
   JSONObject js_row = results.getJSONObject(n);
   // ...
}
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Why in java code converting JSONObject to JSONArray and then get elements of that array what the elements of that array? it just one element john and please complete your codes to try them – JEREEF Dec 17 '15 at 09:33
  • @JEREEF I dont understand you, your server must return valid json data, this exception was thrown because it was not returning it. The simplest form of json with 'john' would look like `{name:"john"}` – marcinj Dec 21 '15 at 12:51