Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends Activity{
- HttpURLConnection connection;
- EditText text_identifier;
- EditText text_password;
- /* Some source code that calls connect()*/
- private boolean connect() throws IOException{
- StringBuilder sb = new StringBuilder("");
- sb.append("username=").append(text_identifier.getText());
- sb.append("&password=").append(text_password.getText());
- String urlParameters = sb.toString();
- byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
- int postDataLength = postData.length;
- String request = "http://serverIPAdress:8080/MyProject/login";
- URL url = new URL(request);
- connection = (HttpURLConnection) url.openConnection();
- connection.setConnectTimeout(3000);
- connection.setRequestMethod("POST");
- connection.setDoOutput(true);
- connection.setInstanceFollowRedirects(false);
- connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- connection.setRequestProperty("charset", "utf-8");
- connection.setRequestProperty("Content-Length", Integer.toString( postDataLength));
- connection.setUseCaches(false);
- OutputStream output = null;
- try {
- output = connection.getOutputStream();
- output.write(postData);
- } finally{
- output.close();
- }
- InputStream response = connection.getInputStream();
- String myString = IOUtils.toString(response, "UTF-8");
- if(myString.equals("true"))
- return true;
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement