有一个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