有一個WCF從數據庫中獲取記錄,並以JSON格式返回像這樣我的項目:Android的價值... java.lang.String類型不能轉換成JSONArray
{"GetNotesResult":"[{\"ID\":1,\"Title\":\"Note 1\",\"Content\":\"Hello Vu Chien Thang\",\"CreatedBy\":\"thangvc\"},{\"ID\":2,\"Title\":\"Note 2\",\"Content\":\"Hello Nguyen Thi Ngoc\",\"CreatedBy\":\"thangvc\"}]"}
我也有一個機器人應用程序消耗的JSON,這是我的代碼:
private JSONArray getNotes(String UserName, String Password) {
JSONArray jarray = null;
JSONObject jobj = null;
try{
StringBuilder builder = new StringBuilder(URL);
builder.append("UserName=" + loggedInUser.getUserName());
builder.append("&");
builder.append("Password=" + loggedInUser.getPassword());
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(builder.toString());
HttpResponse response = client.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
if(status==200)
{
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity,"utf-8");
jobj = new JSONObject(data);
jarray = jobj.getJSONArray("GetNotesResult");
}
else
{
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
catch(ClientProtocolException e)
{
Log.d("ClientProtocol",e.getMessage());
}
catch(IOException e)
{
Log.d("IOException", e.getMessage());
}
catch(JSONException e)
{
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Log.d("Unhandle Error", e.getMessage());
}
return jarray;
}
我在jarray = jobj.getJSONArray("GetNotesResult");
設置斷點,並從JSONException
此消息:
Value [{"ID":1,"Title":"Note 1","Content":"Hello Vu Chien Thang","CreatedBy":"thangvc"},{"ID":2,"Title":"Note 2","Content":"Hello Nguyen Thi Ngoc","CreatedBy":"thangvc"}] at GetNotesResult of type java.lang.String cannot be converted to JSONArray
我試圖複製JSON字符串並粘貼到在線JSON解析器網站http://jsonviewer.stack.hu/並且解析得很好。請幫我解決這個問題!
這JSON你長了? – kyogs 2013-04-11 09:39:07
這是不正確的JsonArray – Sameer 2013-04-11 09:41:12
當然,我已經添加INTERNET permssion清單文件。我甚至登錄到我的android應用程序與另一個函數連接到服務器 – user2269607 2013-04-11 09:43:28