3

I'm using AsyncHttpClient library to make HTTP requests from a very basic Android app. For now, I just need to make a POST request with a JSON body (and that's a mandatory constraint, since the REST services I have to use expect a request in that format) containing a username and a password.

Not knowing much about Android development and the library in question I tried to make a simple GET request to Google, and it perfectly worked. Then, I tried to switch to a POST request but it seems from the documentation that the post method needs strictly a RequestParams parameter. I really need to send a JSON: is there a way to do so with AsyncHttpClient? I tried several solutions found both on the web and on StackOverflow, but unfortunately no one worked.

Ultimately, as a last chance, I'm willing to switch library (and suggestions in this direction would be welcome, too - at least if they're easy-to-use as AsyncHttpClient is, considering my inexperience), but I would prefer to stick to my current choice.

Thanks for your help.

mariuss
  • 1,177
  • 2
  • 14
  • 30
M-elman
  • 313
  • 5
  • 16

1 Answers1

5

first, i suggest u to use retrofit library, it's simple, useful and sweet
but for now, we should to know that how do you do your post,
for example, do you test this:

private static AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("param1", "Test"); 
client.post(Url, params, responseHandler);
JSONObject jsonParams = new JSONObject();
jsonParams.put("param1", "Test");
StringEntity entity = new StringEntity(jsonParams.toString());
client.post(context, Url, entity, "application/json",responseHandler);
Farrokh
  • 1,167
  • 1
  • 7
  • 18
  • Your solution is very close to the one I finally adopted and which worked. Anyway, for this to work, is essential the inclusion of *cz.msebera.android.httpclient.entity.StringEntity*: if you import StringEntity from another library it won't match the signature of the post method declared by AsyncHttpClient (this was the reason why it didn't work for me). – M-elman Oct 07 '18 at 15:49