mardi 30 décembre 2014

Problems converting a utf-8 encoded text to a json object. Json object not utf-8 encoded correctly

I am using a webservice in my Android application (which is utf-8 encoded, i am assuming this also applies to the SQLite Database that comes prepackaged with the Anddroid project) to download, parse and i am extracting the strings form the tags and saving it in my SQLite database on the mobile device. When i convert my custom objects into an arraylist and converting this to a json object, when i submit this to POST (to subsquently store in the remote MySQL database) I am getting a malformed UTF-8 characters. please can anyone help?



// recalling my arraylist of custom object form SQLite database

ArrayList list; list = recallSharedListFromDb();




//Converting arralyist to gson object
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));

//submitting to server
JSONObject json = jsonParser.makeHttpRequest(url_register_code, "POST", param);

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


My php file:



//Get JSON posted by Android Application
$json[] = $_POST["jsonObj"]; // this works...

foreach ($json as $string) {
echo 'Decoding: ' . $string;
json_decode($string);

switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}

echo PHP_EOL;


}


An example of a typical custom object I have in my arrayList is below. I have noticed that when a character like "©" is present in my attribute, I get the "JSON_ERROR_UTF8" error and my program breaks. I have been working on this for days and i am really stuck. Untill i get pass this bit I cannot convert the json object into an array where i can then grab the attributes and place them into my remote MYSQL database.



[{"abstractText":"Ebola viruses and Marburg viruses include some of the most virulent and fatal pathogens known to humans. These viruses cause severe haemorrhagic fevers, with case fatality rates in the rangeola viruses and Marburg viruses include some of the most virulent and fatal pathogens known to humans. These viruses cause severe haemorrhagic fevers, with case fatality rates in the range 25-90%. The diagnosis of filovirus using formalin-fixed tissues from fatal cases poses a significant challenge. The most characteristic histopathological findings are seen in the 25-90%. The diagnosis of filovirus using formalin-fixed tissues from fatal cases poses a significant challenge. The mosola viruses and Marburg viruses include some of the most virulent and fatal pathogens known to humans. These viruses cause severe haemorrhagic fevers, with caseola viruses and Marburg viruses include some of the most virulent and fatal pathogens known to humans. These viruses cause severe haemorrhagic fevers, with case fatality rates in the range 25-90%. The diagnosis of filovirus using formalin-fixed tissues from fatal cases poses a significant challenge. The most characteristic histopathological findings are seen in the fatality rates in the range 25-90%. The diagnosis of filovirus using formalin-fixed tissueola viruses and Marburg viruses include some of the most virulent and fatal pathogens known to humans. These viruses cause severe haemorrhagic fevers, with case fatality rates in the range 25-90%. The diagnosis of filovirus using formalin-fixed tissues from fatal cases poses a significant challenge. The most characteristic histopathological findings are seen in the s from fatal cases poses a significant challenge. The most characteristic histopathological findings are seen in the t characteristic histopathological findings are seen in the liver; however, the findings overlap with many other viral and non-viral haemorrhagic diseases.","authorString":"Martines RB, Ng DL, Greer PW, Rollin PE, Zaki SR.","issue":"2","journalTitle":"J. Pathol.","pageInfo":"153-174","pmid":"25297522","pubYear":"2015","title":"Tissue and cellular tropism, pathology and pathogenesis of Ebola and Marburg viruses.","volume":"235"}]



Aucun commentaire:

Enregistrer un commentaire