这是我的HTML代码
<script type="text/javascript">
$(document).ready(function (){
$.getJSON("/tasks.json", function(data){
$.each(data, function(index, d){
alert(d.alias);
});
})
});
</script>
这是工作......
这是我的安卓代码
HttpEntity entity = null;
HttpEntity temp = response.getEntity();
if(temp != null) {
entity = new BufferedHttpEntity(temp);
responseBody = EntityUtils.toString(temp, HTTP.UTF_8);
System.out.println(responseBody);
}
有时,EntityUtils.toString(temp, HTTP.UTF_8)
会返回一个空字符串。
大多数情况下,我得到了一个例外IllegalStateException:content has been consumed
请帮忙......
/ * ** * **** CODE1 的 * ** * ** * * /
@ResponseBody
@RequestMapping(value="{alias}/{status}", method = RequestMethod.GET)
public List<Push> getShopInJSON(@PathVariable String alias,@PathVariable String status) {
List<Push> results ="uncompleted".equalsIgnoreCase(status) ? pushService.findAllUncompleted(alias) :
"all".equalsIgnoreCase(status) ? pushService.findByAlias(alias) : new ArrayList<Push>();
return results;
}
这次我使用spring Controller,它适用于我的android app json请求..
但是这个...... / * ** * **** CODE2 的 * * * * ** * * /
<%@ page import="java.util.List" %>
<%@ page import="com.easy.push.data.Push" %>
<%@ page import="com.easy.push.service.PushService" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.io.IOException" %>
<%!
private HttpServletResponse response;
private PushService pushService;
private String status="uncompleted";
private String alias="15072203031";
Object execute() throws IOException {
List<Push> results ="uncompleted".equalsIgnoreCase(status) ? pushService.findAllUncompleted(alias) :
"all".equalsIgnoreCase(status) ? pushService.findByAlias(alias) : new ArrayList<Push>();
response.getWriter().write(gson.toJson(results));
return results;
}
%>
它适用于我的webapp,但Android应用程序无法运行..
CODE1适合webapp,android
CODE2适合webapp
为什么CODE2无法处理我的Android代码
请大家......
答案 0 :(得分:0)
尝试这种方式 创建此方法
public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
return buffer.toString();
}
在您的代码中
HttpEntity entity = null;
HttpEntity temp = response.getEntity();
if(temp != null) {
entity = new BufferedHttpEntity(temp);
responseBody = getResponseBody(temp);
System.out.println(responseBody);
}