我試圖發送一個JSONArray
到我的服務器,我的PHP腳本將採取值和搜索我的數據庫匹配,但我不能讓它發送。我不斷收到一個JSONException
發佈一個JSON數組到PHP
「錯誤分析數據org.json.JSONException: java.lang.String類型的值不能轉換爲JSONObject的」
如果我做了vardump在服務器上它顯示爲空。
我有一個像所示的陣列:
ArrayList myList:
["BoUsQJP", "ldT6brS", "iVRuJvmP"]
我用下面的代碼來創建對象,發送給我的JSON解析器。我使用「腳本」爲php腳本服務器端來處理它。
public JSONObject checkInList(ArrayList<String> myList) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", checkinlist_tag));
params.add(new BasicNameValuePair("refids", myList.toString()));
JSONObject json = jsonParser.getJSONCheckList(checkinlistURL, params);
System.out.println("Show me....." + params);
return json;
}
JSON解析器代碼:
public JSONObject getJSONCheckList(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
我缺少什麼?
你的json是什麼樣的?據我所知,json沒有'\ n' –