How to give a json string to a webservice method

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to give a json string to a webservice method

Asuka
Hi there,

I use a jersey webservice and I want to give the webservice method a jsonstring.

this is the method:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/search")
public String search(@QueryParam("token") String token,
        @QueryParam("searchString") String searchParams)

so I define my searchString of the Type JSONObject in the client and call the webservice method:
 
HttpGet requesttest = new HttpGet("http://test.de/myresource/search?token="+cursor.getString(0)+"&searchString="+searchString);
           
but trying this throws the exception that there is a illegal character in the query. I´m sure it is because of :

If I change to post it looks like this:
HttpPost requestPost = new HttpGet("http:/test.de/myresource/search?");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("token", cursor.getString(0)));
params.add(newBasicNameValuePair("searchString",searchString.toString()));

but I don´t know how to use the searchstring in the search method of the webservie now.    

does anybody know how to solve the problem?

best wishes
Reply | Threaded
Open this post in threaded view
|

Re: How to give a json string to a webservice method

Pavel Bucek-2
Please see
http://java.net/projects/jersey/lists/users/archive/2011-04/message/56 
for my reply to your initial post. You probably are not member of
[hidden email], so responses are not sent to your mailbox.

Regards,
Pavel

On 04/07/2011 12:06 AM, Asuka wrote:

> Hi there,
>
> I use a jersey webservice and I want to give the webservice method a
> jsonstring.
>
> this is the method:
>
> @GET
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.APPLICATION_JSON)
> @Path("/search")
> public String search(@QueryParam("token") String token,
>          @QueryParam("searchString") String searchParams)
>
> so I define my searchString of the Type JSONObject in the client and call
> the webservice method:
>
> HttpGet requesttest = new
> HttpGet("http://test.de/myresource/search?token="+cursor.getString(0)+"&searchString="+searchString);
>
> but trying this throws the exception that there is a illegal character in
> the query. I´m sure it is because of :
>
> If I change to post it looks like this:
> HttpPost requestPost = new HttpGet("http:/test.de/myresource/search?");
> List params = new ArrayList();
> params.add(new BasicNameValuePair("token", cursor.getString(0)));
> params.add(newBasicNameValuePair("searchString",searchString.toString()));
>
> but I don´t know how to use the searchstring in the search method of the
> webservie now.
>
> does anybody know how to solve the problem?
>
> best wishes
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/How-to-give-a-json-string-to-a-webservice-method-tp6247814p6247814.html
> Sent from the Jersey mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: How to give a json string to a webservice method

Asuka
Thanks for your answer. Yes, that's right. sending data as a part of the url is not the best way.

The problem is that, I have to use the apache http client, because I send the request from an android app and I had some problems using the jersey client. Sorry, I've had to told directly.

So I 'm not sure what to do now. Do you have any other ideas?

Thanking you in anticipation
Greetings
Asuka



Reply | Threaded
Open this post in threaded view
|

Re: How to give a json string to a webservice method

Pavel Bucek-2
Hmm, we haven't investigated android platform yet, so I don't really
know what is the problem there, but some other users might have
experience with that, so.. feel free to comment.

And or your case - you can put an entity to your request when using
apache http client:

HttpPost httppost = new HttpPost("http://localhost/action.do");
httppost.setEntity(entity);

see [1] for additional details.

Pavel

[1]
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d4e93

On 4/7/11 12:53 PM, Asuka wrote:

> Thanks for your answer. Yes, that's right. sending data as a part of the url
> is not the best way.
>
> The problem is that, I have to use the apache http client, because I send
> the request from an android app and I had some problems using the jersey
> client. Sorry, I've had to told directly.
>
> So I 'm not sure what to do now. Do you have any other ideas?
>
> Thanking you in anticipation
> Greetings
> Asuka
>
>
>
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/How-to-give-a-json-string-to-a-webservice-method-tp6247814p6249477.html
> Sent from the Jersey mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: How to give a json string to a webservice method

Asuka
That's very helpful. Thank you :)