Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. <uses-permission android:name="android.permission.INTERNET" />
  2.  
  3. public class PostRequest extends AsyncTask<String, Void, String> {
  4. @Override
  5. protected String doInBackground(String... strings) {
  6.  
  7. String address = strings[0];
  8. String username = strings[1];
  9. String password = strings[2];
  10.  
  11. try {
  12. URL obj = new URL(address);
  13. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  14. con.setRequestMethod("POST");
  15. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  16. String urlParameters = "username=" + username + "&password=" + password;
  17. con.setDoOutput(true);
  18. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  19. wr.writeBytes(urlParameters);
  20. wr.flush();
  21. wr.close();
  22. BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  23. String inputLine;
  24. StringBuffer response = new StringBuffer();
  25. while ((inputLine = in.readLine()) != null) {
  26. response.append(inputLine);
  27. }
  28. in.close();
  29. return response.toString();
  30. } catch (Exception e) {
  31. // catch exceptions
  32. }
  33. return null;
  34. }
  35. }
  36.  
  37. String responce = new PostRequest().execute("http://www.example.com/script.php", "username", "password").get();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement