Communication of Android App with server

227 views Asked by At

I have my android application. I have no idea how to establish communication between the server and the application. I have a login page in the application. I want to send username and password to the server and return yes for valid input and no for invalid input.

Please tell me how to code at server as I have a public ubuntu server but I don't know what to do there in order to establish communication.

Also, what to write at the application code to send data to the server. What will be the URL of the request I have no idea. Like I have a public server IP 21.4.3.5 with username : ABC and password : XYZ . Now what will be the URL to send request to the server through the application and to receive the response?

1

There are 1 answers

3
Maveňツ On

Try this way

URL url = new URL("http://example.sitedemo.service.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
Uri.Builder builder = new Uri.Builder().appendQueryParameter("username", "maven")
                                       .appendQueryParameter("password", "123");
String query = builder.build().getEncodedQuery();

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();

conn.connect();

InputStream in = new BufferedInputStream(conn.getInputStream());
response = IOUtils.toString(in, "UTF-8");