当前位置: 代码迷 >> Android >> android获取jsp页面输出的json报文报错
  详细解决方案

android获取jsp页面输出的json报文报错

热度:56   发布时间:2016-04-28 01:01:30.0
android获取jsp页面输出的json报文出错
如题所示,很奇怪,我在jsp页面上out.print打印出来的json报文字符串,但通过android那边获取,得到的不是打印出来的报文,却是html的布局代码!!我在网上查了很多,都是一样的实现方式,为什么我得到的不是打印出来的信息而是html页面代码呢?我android端处理的方式如下:
servicePath="http://"+servicePath;
String resultStr="";
try
{
 HttpClient httpclient=new DefaultHttpClient();
 HttpGet get = new HttpGet(servicePath);
 HttpResponse response=httpclient.execute(get);
 resultStr=EntityUtils.toString(response.getEntity());
}
 catch(Exception e)
 {
 e.toString();
 }



return resultStr;

servicePath是tomcat下的一个jsp页面,页面就是打印一个json格式的信息,代码如下
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <%
    
    String str="{\"1\":{\"0\":1,\"1\":\"\\u754c\\u9762\\u5305\",\"2\":\"jy\",\"3\":\"ui\",\"4\":\"201304261655227\",\"5\":\"/hotelcms/tvui/83/201304261619010407u53cdu9988.docx\"},\"2\":{\"0\":2,\"1\":\"\\u5ba3\\u4f20\\u89c6\\u9891\",\"2\":\"replace\",\"3\":\"vedio\",\"4\":\"201304261608122\",\"5\":\"\"},\"3\":{\"0\":3,\"1\":\"\\u9152\\u5e97\\u56fe\\u6807\",\"2\":\"replace\",\"3\":\"logo\",\"4\":\"201304261605123\",\"5\":\"abc.zip\"},\"4\":{\"0\":4,\"1\":\"dish\",\"2\":\"jy\",\"3\":\"dish\",\"4\":\"201304211102124\",\"5\":\"abc.zip\"},\"5\":{\"0\":5,\"1\":\"goods\",\"2\":\"jy\",\"3\":\"goods\",\"4\":\"201304211102125\",\"5\":\"abc.zip\"},\"6\":{\"0\":6,\"1\":\"movie\",\"2\":\"jy\",\"3\":\"movie\",\"4\":\"201304211102126\",\"5\":\"abc.zip\"},\"7\":{\"0\":7,\"1\":\"tvlogo\",\"2\":\"jy\",\"3\":\"tvlogo\",\"4\":\"201304211102127\",\"5\":\"abc.zip\"}}";
    out.print(str);
    
     %>
  </body>
</html>

------解决思路----------------------
你获取到的本来就是html,你直接返回json格式数据即可,不用写在html里面。
------解决思路----------------------
应该访问一个servlet或者一个.do这类文件返回一个json格式的数据
------解决思路----------------------
引用:
Quote: 引用:

你获取到的本来就是html,你直接返回json格式数据即可,不用写在html里面。

不太清楚你的意思,怎么直接返回json数据呢?我是在jsp里面out.print出的json串。这样不行?怎么才能获取到json串而不是html代码呢?谢谢

你用str代替你的html即可