문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top