0

这是我的 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 控制器,它适用于我的 android 应用程序 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;
    }
%>

它适用于我的 web 应用程序,但 android 应用程序不起作用..

CODE1 适用于 webapp、android

CODE2 适用于 webapp

为什么 CODE2 不能在我的 android 代码上工作

谢谢大家。。

4

1 回答 1

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);
        }
于 2013-09-04T09:58:18.663 回答