1

I am sending a Json representation of an arraylist of custom objects to the server. I hope then to take that json object, decode into an array and retrieve the attributes and place them in MySQL. I think I am having an encoding issue. My php source code is written in notepad++ and is endcoded to utf-8. I believe Android encodes its source code and SQlite Datavabe in utf-8. The data that i use to construct the arraylist of my custom objects comes from SQLite.

When i POST a json object of my custom arraylist on the browser, the json is read successfully. However, when i send it via my android I get the error below. Someone please help?

Android code:

ArrayList<Article> list;
   list = recallSharedListFromDb();

    // Create json
    Gson gson = new GsonBuilder().create();
    String json_arrayList = gson.toJson(list);
        // Build parameters. 
        List<NameValuePair> param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("jsonObj", json_object));

        JSONObject json = jsonParser.makeHttpRequest(url_register_code, "POST", param);

        // Read json response coming from server.
        Log.d("SyncToMySQL", "json string: "+ json.toString());

Php code:

//Get JSON posted by Android Application $json = $_POST["jsonObj"]; // this works... //Decode JSON into an Array $data = json_decode($json);

 $num_objects = count($data);

if($data){
$response["number of objects"] = $num_objects;
$response["success"] = 1;
echo $json. "<br><br>";;
 }else{
$response["number of objects"] = 0;
$response["success"] = 0;
 }

echo json_encode($response);

StackTrace:

  12-31 09:54:59.966: E/JSON Parser(1826): Error parsing data org.json.JSONException: Value (STRANGE LOOKING SYMBOLS) of    type java.lang.String cannot be converted to JSONObjec
gsanskar
  • 673
  • 5
  • 10
derek
  • 25
  • 7

2 Answers2

0

Missing header before sending data:

header('Content-type: application/json');
echo json_encode($response);; 
Kamil Adryjanek
  • 3,238
  • 1
  • 20
  • 21
0

Add header('Content-type: application/json'); before sending json, like this

$num_objects = count($data);

if($data){
$response["number of objects"] = $num_objects;
$response["success"] = 1;
echo $json. "<br><br>";;
 }else{
$response["number of objects"] = 0;
$response["success"] = 0;
 }

header('Content-type: application/json');
echo json_encode($response);
puttu
  • 969
  • 7
  • 10