tags:

views:

66

answers:

2

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

+1  A: 

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.

CommonsWare
I only consume it, didn't wrote it. How would you recommended do proceed in this case? Download the image directly? Change something in web service?
Alin
And on a second note, there is no need to be so hard. We are learning and experiencing. It's easy to say it's wrong, but harder how to make it right... I need to bring a 400x400px image from a website via a secure way and store it on a sqlite table and display it on a imageview. Web service can be changed of required.
Alin
@alin: The Base64+JSON adds no value and takes CPU time on both sides, which is particularly precious on a phone. It also increases your bandwidth, which users on metered data plans care about a fair bit. Download the original image from a plain URL and store it in a BLOB column in SQLite.
CommonsWare
+1  A: 

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.

chubbard