在应用中android需要调用远程WCF,参数数据传给了WCF,也返回了结果数据,然后郁闷的事发生了:利用返回的数据构建JSONObject总是报错,提示“WARN/System.err(489): org.json.JSONException: Value {"name":"Leon","sex":"male"} of type java.lang.String cannot be converted to JSONObject“,可我在debug中把{"name":"Leon","sex":"male"} 直接用于JSONObject是不报错的(new JSONObject({"name":"Leon","sex":"male"} );),求高人相助。部分代码如下:
jsonObject.put("usercode", "test");
jsonObject.put("password", "test");
ByteArrayEntity se = new ByteArrayEntity(jsonObject.toString().getBytes("UTF8"));
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
response = httpclient.execute(httppost);
if(response != null){
//InputStream in = response.getEntity().getContent();
String t = new String(EntityUtils.toString(response.getEntity()).getBytes("UTF8"));
//String t = convertStreamToString(in);
//String tt = t;
JSONArray json = new JSONArray(t); (错误产生的地方)
jsonObject.put("usercode", "test");
jsonObject.put("password", "test");
ByteArrayEntity se = new ByteArrayEntity(jsonObject.toString().getBytes("UTF8"));
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
response = httpclient.execute(httppost);
if(response != null){
//InputStream in = response.getEntity().getContent();
String t = new String(EntityUtils.toString(response.getEntity()).getBytes("UTF8"));
//String t = convertStreamToString(in);
//String tt = t;
JSONArray json = new JSONArray(t); (错误产生的地方)
解决方案 »
- ListView中的button问题
- ExpandableListview中的onItemClickLIstener无反应
- 如何启动事先地未配置的指定的任意 Android/iOS App
- android 竖着的进度条ProgressBar(急急急急急急急急急)
- android 线程 sleep 的问题
- 求助关于读取xml文件的问题
- android phone初始化时与底层的信息传递
- 怎么在对话框的Messages里添加图片呢?
- ophone开发中如何封装某一功能
- 初级问题, 找不到我添加手势库文件... 100分
- 关于HorizontalScrollView滚动后获取焦点的问题,谢谢
- 大家一般用什么java框架开发restful webservice
但你从网络上获取下来的却是" {"name":"Leon","sex":"male"} " 这样的一个string
你那代码相当于执行: JSONObject("{"name":"Leon","sex":"male"} ")
应该不string类型值先放入JSONObject,再放入JSONArray
JSONArray json = new JSONArray(t);
这样试试
String result= EntityUtils.toString(response.getEntity());
try {
JSONObject json=new JSONObject(result);
JSONArray array=new JSONArray(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}