android request json data:content has been consumed -
this html code
<script type="text/javascript"> $(document).ready(function (){ $.getjson("/tasks.json", function(data){ $.each(data, function(index, d){ alert(d.alias); }); }) }); </script>
it's work...
and android code
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); }
sometimes, entityutils.tostring(temp, http.utf_8)
returns empty string. mostly, got exception illegalstateexception:content has been consumed
please help...
/********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; }
this time use spring controller,it's works android app json request..
but this.. /********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; } %>
it's work webapp,but android app not work..
code1 webapp,android
code2 webapp
why code2 not working on android code
tks everyone..
try way create method
public string getresponsebody(final httpentity entity) throws ioexception, parseexception { if (entity == null) { throw new illegalargumentexception("http entity may not null"); } inputstream instream = entity.getcontent(); if (instream == null) { return ""; } if (entity.getcontentlength() > integer.max_value) { throw new illegalargumentexception( "http entity large 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); } } { instream.close(); reader.close(); } return buffer.tostring(); }
in code
httpentity entity = null; httpentity temp = response.getentity(); if(temp != null) { entity = new bufferedhttpentity(temp); responsebody = getresponsebody(temp); system.out.println(responsebody); }
Comments
Post a Comment