Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public class LoginActivity extends Activity{
  2.  
  3. HttpURLConnection connection;
  4. EditText text_identifier;
  5. EditText text_password;
  6.  
  7. /* Some source code that calls connect()*/
  8. private boolean connect() throws IOException{
  9. StringBuilder sb = new StringBuilder("");
  10. sb.append("username=").append(text_identifier.getText());
  11. sb.append("&password=").append(text_password.getText());
  12. String urlParameters = sb.toString();
  13. byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
  14. int postDataLength = postData.length;
  15. String request = "http://serverIPAdress:8080/MyProject/login";
  16. URL url = new URL(request);
  17. connection = (HttpURLConnection) url.openConnection();
  18. connection.setConnectTimeout(3000);
  19. connection.setRequestMethod("POST");
  20. connection.setDoOutput(true);
  21. connection.setInstanceFollowRedirects(false);
  22. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  23. connection.setRequestProperty("charset", "utf-8");
  24. connection.setRequestProperty("Content-Length", Integer.toString( postDataLength));
  25. connection.setUseCaches(false);
  26. OutputStream output = null;
  27. try {
  28. output = connection.getOutputStream();
  29. output.write(postData);
  30. } finally{
  31. output.close();
  32. }
  33. InputStream response = connection.getInputStream();
  34. String myString = IOUtils.toString(response, "UTF-8");
  35. if(myString.equals("true"))
  36. return true;
  37. else
  38. return false;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement