Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. //on create
  2.  
  3. Button button = (Button) findViewById(R.id.button);
  4.  
  5. login = findViewById((R.id.Login));
  6. password = findViewById(R.id.Password);
  7. button.setOnClickListener( new View.OnClickListener() {
  8.  
  9. @Override
  10. public void onClick(View v) {
  11. sendPost();
  12. try {
  13. TimeUnit.SECONDS.sleep(2);
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. sendPost();
  18. }
  19. });
  20.  
  21.  
  22.  
  23. public void sendPost() {
  24. Thread thread = new Thread(new Runnable() {
  25. @Override
  26. public void run() {
  27. try {
  28. URL url = new URL("http://cargoalgps.cba.pl/api/uzytkownik/login.php");
  29. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  30. conn.setRequestMethod("POST");
  31. conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  32. conn.setRequestProperty("Accept","application/json");
  33. conn.setDoOutput(true);
  34. conn.setDoInput(true);
  35.  
  36. JSONObject jsonParam = new JSONObject();
  37. jsonParam.put("login", login.getText());
  38. jsonParam.put("haslo", password.getText());
  39.  
  40. Log.i("JSON", jsonParam.toString());
  41. DataOutputStream os = new DataOutputStream(conn.getOutputStream());
  42. //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
  43. os.writeBytes(jsonParam.toString());
  44.  
  45. os.flush();
  46. os.close();
  47. if(conn.getResponseCode() == 200){
  48. text.setText("zalogowano");
  49. }
  50. else{
  51. text.setText("bledne dane");
  52. }
  53.  
  54.  
  55. conn.disconnect();
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. });
  61.  
  62. thread.start();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement