I am polling my webservice and receiving a response which is very long (its supposed to be a json)
but because it's too long, the system truncates the end of it
and i receive a json that looks like this {"object":["one","two", ....
which is of course not a valid json anymore.
3 dots instead of the correct json ending.
There is nothing i can do about the length of the json.
Is there anything i can do to receive all of it?
I think you are getting confused between what you can print in debug, and this can be truncated by Android logs, and what you exactly receive from the server. Either try to print out what you get as a response in a file (on sdcard) or just print out the length of the received stuff.
But there are no chances that a response is truncated in http, think about downloading a huge file of several Mb, except if there is really something very wrong on the server side, like a bad content-length.
Related
I send a request off to our server and receive a response from the server. The response contains information related to an image. The server returns the image as a base64 string. It is taking way to long (30-40 seconds per image) to parse out the data so I can pull the information I need to save and view the image. I am looking for suggestions on better ways that might help to speed up the process. I am currently using a SAX parser and the slow down is when the .parse function is called.
I am using this question's source code How to asynchronous perform a httprequest and show the progress of downloading the response in order to fetch a webpage's html source and display a progressbar with the downloaded data so far and how much the total size of the page is.
That particular code doesn't actually work on ICS 4.0.3 because clHeaders[0] doesn't point to anything and an exception is raised called ArrayIndexOutOfBounds at index 0 because the Content-Length header is omitted from the server's response. I tried using getContentLength in case that was the problem - it returned a negative value of -1 then I iterated over all the headers and the Content-Length was not there. After removing those bits, the code works fine and a webpage is fetched, written to a file and the size as it is being downloaded is displayed but obviously not the end size till I actually download it all.
I only have three ideas of the cause:
I am not sending a Content-Length header thus I am not receiving one either - but this sounds wrong. Plus I have no idea if the HttpClient isn't sending one in the background.
I read here in another question that if the response of the server was streaming or chunked then getContentLength can return -1.
Gzip? But I have no idea how to disable it or if it was enabled in the first place
If you are thinking it's the server that is broken, I tried many websites including Google and still no Content-Length header whatsoever.
Content-Length header is optional, it's mostly only useful for HEAD request or for very large data where the client might decide they want to abort the request if the content is too large. So yes, Content-Length dies not always exist. Other than that, you should generally just get the content length by reading the data.
There are cases where the Content-Length should not be included in the response or should be ignored. These cases are documented on the W3 site. If your content has a transfer encoding of gzip for example, the content-length should be ignored.
I am sending a POST request to a web-service which returns data in XML format. When I invoke the web-service via the browser i can see the complete XML file. However, when I call the web-service from my android app (in emulator) I only get partial XML data?
The size of the XML file being sent is approx 14500 bytes. I first store the response in a byte array whose size i calculate using getContentLength() function. This method returns the correct size of the response.
However, my InputStreamReader reads approximately 6350 bytes of the actual data to the byte array. The remaining part of the byte array simply contains zeros.
I tried looking everywhere online but so far I haven't come across a solution. Yes chunking is one option but since I dont have access to the web-service code I can't implement that option.
Really appreciate if someone could guide me here!
I don't understand why do you need to use InputStreamReader (which can behave very wrong if you are not careful) to read web service data (in your case it is only around 14.5kb). Just use HttpURLConnection, connect to your ws, and it will take care for you about the connection and also the response connection.getResponseMessage();
I make a soap request in my Android app using HttpURLConnection the response is a base64 encoded string holding the data of an image.
the problem is that the response always received in complete. so the image can not be constructed correctly.
what can be the reason for this ?
thanks
A friend of mine blogged about this a year or so ago. Base64 is supposed to be built in, but isn't/wasn't? He has details here.
Possibly server is sending the incorrect Content-Length header.
Did you write the server code?
Happened to me once when I was reporting the content length based on the size of buffer not the actual size of data inside buffer.
I have the following code:
String response = webService.webGet(""); the response of the web service
String LargeImage = new Gson().fromJson(response,String.class);
byte[] imageByteArray = Base64.decode(LargeImage);
response is like: "iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCA... " a Base64 encoded image having around 400.000 characters.
The response comes very quick from the web service. When it tries to parse it with Gson after a while (like 20-30 seconds) I get an error with out of memory. How can I parse this simple string without Gson, it should be fairly simple but I don't know how to do it.
Please advise me. Thank you for your time
Change your Base64 image encoding into a URL where you can download the image directly.
Then go get flexjson 2.1 and you can parse JSON on Android very easily in a few lines. It's also faster than GSON.
http://flexjson.sourceforge.net
But from your post it looks like you're just sending the Base64 image over JSON as a single string. No need to use JSON in that case. Unless you plan on wrapping some metadata around it in the future.
Also skip storing the image in the DB. Just write it to the filesystem, and put the file path in the DB linked to your object. Much easier to debug when you wonder what image you downloaded, etc.
response is like: "iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCA... " a Base64 encoded image having around 400.000 characters.
You have got to be kidding me.
Please advise me.
Find a sensible Web service and switch to it. If somebody at your firm wrote the Web service, fire them. If you wrote the Web service, fire yourself. I see no need to be returning an image -- particularly one that massive -- in Base64 encoding, wrapped in JSON.
In the meantime, you could try the built-in Android JSON parser rather than Gson.