[Answer]-Android authentication using JSON

0👍

Okay, so I’m now answering my own question 😀

The issue was with the path variable in the URL string.
This is the format of one of the URL constructors based on this document.
URL(String protocol, String host, int port, String file)

Since I am posting the JSON to the /user path, that’s the one I insert into the constructor as the directory.
So, my URL was formed like so:
URL url= new URL("http",cons.SERVER,cons.PORT,"/user/");

My mistake in the beginning was using /user instead of /user/
but other than that, the URL structure and connections are all alright.

1👍

If your are building your HttpPostRequest well, and you only want to know how to attach JSON, here you are a possible solution for it:

  StringEntity formEntity = new StringEntity(yourJsonObject.toString());
  yourPostRequest.setEntity(formEntity);

I hope this helps!

PS:In addition, let me recommend you the use of this component:

https://github.com/matessoftwaresolutions/AndroidHttpRestService

I’ve used it in an Android app that is connecting to a python server API and it makes http request easier for your Android client.

Leave a comment