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.
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 writing a rest client application and the way the server has been set up (beyond my control) is to perform specific filters the query string has a raw json attached as follows:
http://www.someurl.com/api/user?filter=[{"field1":"value1","field2":"value2","field3":"value3"}]
Currently I am using Robospice/Spring to handle the network requests and for regular queries (i.e. no json paramters) it works pretty well. However, whenever I try and process a GET request with the above described url, I keep receiving 500 server error. I tried the same request using This android-async-library, which seems to be able to handle the parameters a little better (200 OK etc). This has lead me to believe the issue is with the way the URL is formed/parsed.
My question is can Spring handle URLs of this format? or if anyone knows the best way to handle/encode it that will be usable for spring?
Yes possibly your url should be encoded especially when you post json in parameters(because of ", :, }, ' notations)
use this for creating your url
String url = Uri.parse("http://youradres.com")
.buildUpon()
.appendQueryParameter("filter", "[{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}]")
.build().toString();
It is probably not a good idea. I would pass those values like this:
http://www.someurl.com/api/user?field1=value1&field2=value2&field3=value3
And manage those values on your controller. I think it is more rest-ish. However, going straight to your question. I think the problem is you need to "encode" your URL before you send those values. And then decode it back in your server.
I can not give you code because I don't use Java but this might help you to get on the right track.
I want to upload or post a image file on server using REST WCF. For this I am using JSON as input format.
But when I am checking this post call with debugger due to Base64 image data input is not accepted by the debugger .
I was giving me the 400-Bad Request response for this call.
Thanks.
First understand the concept of JSON/REST.
Then Make your WCF method to input the Byte[] i.e. Byte Array.
Convert your Image in Base64 and Pass it to the REST.
In order to send Image to JSON Web Service, you need to convert that to Byte Array or Base 64 String Representation.
Hope this helps.
I need a workaround for the following task:
I have a JPG (of whatever) picture on my sd card, and I need to send it to another device in the background. How should I do that?
Best way (in theory) would be via MMS, but after a lof of searching, I can say that there is no official and trustful (and working) way to do that in the background.
Any ideas, samples, even proofs that it can be done are welcomed! All that matters is that a remote device must have access to that image.
if you want the sending to happen in background you could use android beam, but you would have to get both devices cloth together.
And as i am not sure about what u mean by background i can't be sure that thats what you want. :)
First of all you need to create a "Service" in App which will run in background and do all tasks given below. A central PHP Server required for this task. Other device can download that file by the same HTTP request method.
Convert image to base64 string--
How to convert a image into Base64 string?
you can convert byte array to suitable types- string or delimiter(, or .) separated string
Then create a HTTP request--
Make an HTTP request with android
for HTTP request create a url like this - https://www.yoursite.com/post/?code="base64 string goes here"
-Receive data in php file on your server by $_GET global array
$code = $_GET['code']
In php file convert base64 code to original image.
How to decode a base64 string (gif) into image in PHP / HTML
get image from base64 string
Maybe I explained badly my needs. An important thing I missed is that the same person has access to both device. I solved it by uploading the image to google drive.
I'm trying to send an image as a Base64 encoded string to my PHP script via HttpGet, but as I kind of expected I get a 414 URI too large from my server.
Is there a way to post large strings with HttpGet?
Any help is greatly appreciated.
URI limit depends on server settings and its not a good idea to send huge data via Get method. And no, you cant use Post on Get service.
The best would be to alter your webservice to receive Post request and then you may send as long data as you want
when the URI is too large (cause of you are using it to send an image...) all you can do is to try to make it smaller, by compressing it. Or if you have access to the server, increase the limit....
the one way or the other.... use http-post instead of http-get.
you wont have the problem that the size is limited (or if theres a limit, its way bigger then the one from http-get) and i cant believe that sending an image via http-get is usage as intended by the http