2014-12-31 49 views
1

我發送一個Json自定義對象arraylist表示到服務器。我希望接下來將這個對象解碼爲一個數組,然後檢索這些屬性並將它們放在MySQL中。我想我有一個編碼問題。我的php源代碼是用notepad ++編寫的,最終編碼爲utf-8。我相信Android會使用utf-8對其源代碼和SQlite Datavabe進行編碼。我用來構建我的自定義對象的數據來自SQLite安卓json到php和回

當我在瀏覽器上自定義arraylist的對象時,我的json被成功讀取。但是,當我通過我的android發送它時,我得到下面的錯誤。有人請幫忙?

的Android代碼:

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代碼:

//獲取JSON張貼由Android應用程序 $ JSON = $ _POST [ 「jsonObj」]; //這項工作... //將JSON解碼爲數組 $ 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); 

堆棧跟蹤:

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 

回答

0

缺少標題發送數據前:

header('Content-type: application/json'); 
echo json_encode($response);; 
+0

恐怕沒有工作。我剪切並粘貼了你的帖子。 – derek

+0

您可以請記錄此JSON響應:Log.d(「JSON:」,json.toString());並粘貼在這裏。 –

+0

12-31 10:47:41.726:D/SyncToMySQL(1826):json字符串:{「對象數量」:0,「成功」:0} – derek

0

發送JSON之前添加header('Content-type: application/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; 
} 

header('Content-type: application/json'); 
echo json_encode($response); 
+0

我仍然收到相同的錯誤 – derek