Advertisement
Guest User

Untitled

a guest
Mar 8th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1.  
  2. public class ConnectionRequest extends AsyncTask<Void, Void, String> {
  3.  
  4.  
  5. private HttpURLConnection conn = null;
  6.  
  7.  
  8. @Override
  9. protected String doInBackground(Void... params) {
  10. String address = "www.example.co.il/login.php" , attr = "username=moshe&password=123";
  11. String answer;
  12. OutputStreamWriter request = null;
  13. try {
  14. URL url = new URL(address);
  15. conn = (HttpURLConnection) *****openConnection();
  16. conn.setDoOutput(true);
  17. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  18. conn.setRequestMethod("POST");
  19. request = new OutputStreamWriter(conn.getOutputStream());
  20. request.write(attr);
  21. request.flush();
  22. request.close();
  23. BufferedReader in = new BufferedReader(
  24. new InputStreamReader(conn.getInputStream()));
  25. String inputLine;
  26. StringBuilder sb = new StringBuilder();
  27. while ((inputLine = in.readLine()) != null)
  28. sb.append(inputLine);
  29. in.close();
  30. answer = sb.toString();
  31. } catch (IOException e) {
  32. } finally {
  33. if (conn != null)
  34. conn.disconnect();
  35. }
  36. return answer;
  37. }
  38.  
  39. @Override
  40. protected void onPostExecute(String params) {
  41. String answer = params;
  42. if(answer.equals("true"))
  43. Toast.makeText(context, "username exists", Toast.LENGTH_SHORT).show();
  44. else
  45. Toast.makeText(context, "username not exists", Toast.LENGTH_SHORT).show();
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement