0

Question is below in bold.

I have a PUT method in my jax-rs which is the following code:

@PUT
@Path("{id}/url.json") 
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public ResponseEntity dealRedeemed(@PathParam("id") long id,
                                    @FormParam("lat") long lat,
                                    @FormParam("lng") long lng) {
    ResponseEntity entity = new ResponseEntity();
    entity.addField("lat", lat); // THIS WORKS WHEN POSTING FROM A FORM
    entity.addField("lng", lng); // THIS WORKS WHEN POSTING FROM A FORM
    return entity;
}

I am calling it from Android using:

AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("lat", latitude);
params.put("lon", longitude);
client.put(getAbsoluteUrl(id+"/url.json"), params, responseHandler);

How can I access the params in JAX-RS when the PUT method is called via Android? If I were to use a HTML form to call this method, it works fine. I the response I get when calling this from an android device is:

HTTP Status 400 - Bad Request : The request sent by the client was syntactically incorrect.

Any help in the right direction would be greatly appreciated. Thanks!

Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
Bhavik P.
  • 905
  • 3
  • 10
  • 28

2 Answers2

0

To read RequestParams when you added them on the client like this:

 RequestParams rp = new RequestParams();
 rp.add("fookey1", "foovalue1");
 rp.add("fookey2", "foovalue2");
 // also with rp.put(key, value);

You can just read it with:

public ResponseEntity dealRedeemed(String requestparams){
}

But the problem with this is that you need to parse the String. What is working easier for me is to send it as headers such like:

client.addHeader("fookey1", "foovalue1");
client.addHeader("fookey2", "foovalue2");
client.post(url, myHandler);

And reading it like:

public ResponseEntity dealRedeemed(@HeaderParam("fookey1") String foovalue1,
@HeaderParam("fookey2") String foovalue2){
//So now you can use the variables independently
}
Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45
-2

INCORRECT: Try

client
    .preparePut(url)
    .addFormParam("lat", latitude)
    .addFormParam("long", longtitude)
    .build();

as described here https://github.com/AsyncHttpClient/async-http-client/blob/master/client/src/main/java/org/asynchttpclient/RequestBuilderBase.java

UPDATE: my answer above is for the wrong project, the correct is loopj.com.

I've looked at the correct project. Probably, you need to use @QueryParam on server-side instead of @FormParam.

I couldn't figure out from their documentation what RequestParams really mean: query params, form params, or request body. Seems like a poor documentation on their side.

Dzmitry Lazerka
  • 1,809
  • 2
  • 21
  • 37
  • Is there something I can do on the server side rather than the APP side? btw: AsyncHttpClient doesnt have a .preparePUT.. could be version I am using. – Bhavik P. Mar 07 '16 at 04:22
  • 1
    @Dzmitry Lazerka: this question was both mistagged and mislabelled, because some Android project is not capable of properly routing to his own project, see https://github.com/loopj/android-async-http/issues/876. Fixing question title and tags: not related to AsyncHttpClient but to android-async-http – Stephane Landelle Mar 07 '16 at 10:26
  • @Bhavik P. So you completely did it wrong. I did my research (I'm the "async-http-client" developper), but neither did you nor the loopj founder when he picked a name for his project and his API. The "async-http-client" tag that you used specifically links to http://github.com/AsyncHttpClient/async-http-client. Because of that, Dzmitry thought you were really using AHC, hence his answer that doesn't match your needs. I retagged it as "android-async-http" which is the proper tag for the project you use. So on the contrary, it doesn't hurt your chances of getting an answer, it improves them. – Stephane Landelle Mar 08 '16 at 18:09
  • yea, I realized I messed up. Sorry about that. You were trying to help and I didn't even realize you edited my post. Thanks Stephane. I am still stuck on this dumb issue. – Bhavik P. Mar 08 '16 at 18:34
  • No pro. And sorry I can't help with this other project. – Stephane Landelle Mar 09 '16 at 09:31