I have the follow开发者_Go百科ing 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.
精彩评论