Passing JSON string in the URL parameters using Spring & Robospice - android

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.

Related

HTTP POST: name/value pairs vs JSON string

We are wondering whether it is better to place parameters of a server method call within name/value pairs in s post, or post a JSON string to be processed by the server. The problem is that in some cases, the length of the parameters varies, and it is easier to handle this with a JSON. Also because we are trying to foresee the case in which our app users using these methods grows a lot and performance becomes an issue.
Which way do you think is the best choice? Is there any other suggestion?
Thank you all in advance!
Best way is to post a JSON. You can pass them in HTTP body and thereby give it some security and invisibility.
And JSON string can be even passed as a url parameter if u want.

Using REST to Send and Receive Complex Data

My client is an Android app and my service is an asp.net web api. I’m the only one using my service. I am trying to duplicate, in the Android-REST world what I am already doing in the Microsoft Windows Phone 7/ WCF SOAP world. I have numerous methods that both receive complex objects and return complex objects.
The WCF-SOAP world is simple. You can pass any complex arguments you want and return any complex results you want. Logically, it’s just a Remote Procedure Call.
But when I post questions about doing the same thing in REST, I’m told I should limit my services into GET, PUT, POST, and DELETE only. And that I should only do what is “proper” according to RFC2616. Some speak of this in almost a religious manner.
Forgeting about the religion, what’s wrong with using a GET for everything? Or what’s wrong with using a POST for everything? What I do does not fall into the simplistic RFC2616 categories. For instance I’m passing a thousand legs of a trip taken in a car and I’m getting back another version of that trip with erratic GPS errors smoothed out. Or, I’m sending a conversation in english and getting that conversation back in German.
In the android client I have the objects I want to send over HTTP already serialized into json strings by using Google-GSON. So my questions are…
How can I send these json strings to my REST Service as arguments in either GET or POST?
Is it possible and feasible to use just all GETs (or all POSTs) for all of my calls to my REST Service and how do I do that?
I have a more pragmatic question about this posted at sending a json string in a http url as I can’t find any examples anywhere of sending json strings over http GET or POST.
Thanks, Gary
Using the good HTTP verb is very usefull to simply know what to do when you request failed (for example) or just to do some specific stuff. If you sent a POST request, it's implicitly suppose that you have to parse your resource in order to obtain a stream which be sent via the request's body. In other hand, when you are retrieving data via GET, it's suppose that the request is gonna be sent back to you as a stream that you will mapped to your model, pojo, or anything else.
I can suggest you to use library such as RESTDroid. You can send POJO and receive POJO. It's a "resource oriented" library, so you can know at any moment if a particular local resource is remotely syncronized. Data persistence between local and remote is automatically handles.
RESTDroid is alpha released. You can have a look to RoboSpice. It's a powerful library to manage REST call but it's up to you to manage the persistency between local and remote resources.
1) The WCF-SOAP world is simple. You can pass any complex arguments you want and return any complex results you want. Logically, it’s just a Remote Procedure Call.
- IN REST:"You can pass any complex arguments you want and return any complex results you want too.
2a) Forgeting about the religion, what’s wrong with using a GET for everything?
In SOAP services WCF/or classical you are wrapping all requests into http POST so using single verb would end up to SOAP or - don't even think about it - your own communication protocol:-D
2b) You can technically compose GET request with non empty body - most of the servers ignore it by default though and it would be technically problematic to read it..
the other part of the question is answered by Pcriulan above

Send parameter in HTTP POST

I am using this code http://stunningco.de/2010/04/25/uploading-files-to-http-server-using-post-android-sdk/ to send an image to my web server the code works fine but i want to be able to send some aditional parameters.
How can i send them , i tried constructing the url request like this http://www.mywebserver.com/postdata?param1=somevalue&param2=somevalue but the web server never reconisez them.
Thanks.
I don't know how did you program your custom server, but you passed the additional parameters the right way (via GET, not POST).
If you did it from scratch, by reading the HTTP protocol packets and interpreting them manually, then you'll have your GET parameters just after the requested URL at the first line, and will have to split the url by first using the '?' character to split the url from the parameters, then the '&' to split the different parameters into an array, and then split each parameter using the '=' character into a key and a value.
If that's not the case and used some kind of pyhon/Java/VB.NET library, please tell us which one..

Send long string (Base64 encoded image) via HttpGet

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

Parse simple string JSon in Android

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.

Categories

Resources