Advertisement
Guest User

Untitled

a guest
Apr 18th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. import androidx.appcompat.app.AppCompatActivity;
  2.  
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.EditText;
  10. import android.widget.ListView;
  11. import android.widget.SimpleAdapter;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import org.json.JSONArray;
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18.  
  19. import java.io.BufferedReader;
  20. import java.io.InputStreamReader;
  21. import java.io.OutputStream;
  22. import java.net.URL;
  23. import java.net.URLConnection;
  24. import java.net.URLEncoder;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27.  
  28. public class LocationsFoundActivity extends AppCompatActivity {
  29.  
  30. String myJSON; // string pre udaje zo servera
  31. JSONArray peoples = null; // pole pre dekodovane udaje
  32. // list do ktoreho sa "prelozia" dekodovane udaje
  33. ArrayList<HashMap<String, String>> personList;
  34. ListView list; // zobrazovaci list
  35. SimpleAdapter adapter;
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_locations_found);
  41. odosliData();
  42.  
  43. }
  44.  
  45. public void odosliData() {
  46. String myUrl = "http://bakalarskapraca.mojawebka.eu/vma_serv/zapis.php";
  47.  
  48. // vytvorenie inštancie
  49. Komunikator sender = new Komunikator();
  50. // odoslanie parametrov
  51. sender.execute(myUrl, "2", "_meno", "_priezvisko", "_ulica","_psc","_mesto","_mail","_teleC");
  52. }
  53.  
  54. public class Komunikator extends AsyncTask<String, Void, String> {
  55.  
  56. @Override
  57. protected String doInBackground(String... params) {
  58. String stringUrl = params[0]; // precitanie URL
  59. int task_type = Integer.parseInt(params[1]); // 2. parameter - co ma urobit
  60. String result = "", riadok;
  61.  
  62. try {
  63. // vytvorenie URL
  64. URL url = new URL(stringUrl);
  65. // pripojenie k danemu URL
  66. URLConnection conn = url.openConnection();
  67. conn.setDoOutput(true); // nastavenie na pristup post
  68. // parametre pripojenia
  69. conn.setReadTimeout(15000); conn.setConnectTimeout(15000);
  70.  
  71. switch (task_type) {
  72. case 1: // vyrob connect - vsetko je pripravene, posle sa len dotaz
  73. conn.connect();
  74. break;
  75. case 2: // prečítame parametre z volania metódy
  76. String meno = params[2];
  77. String priezvisko = params[3];
  78. String ulica = params[4];
  79. String psc=params[5];
  80. String mesto=params[6];
  81. String email=params[7];
  82. String telefonneCislo=params[8];
  83. // prístup k output streamu requestu, vytvorenie parametrov
  84. OutputStream output = conn.getOutputStream();
  85.  
  86. String data =
  87.  
  88. URLEncoder.encode("meno", "UTF-8") + "=" +
  89. URLEncoder.encode(meno, "UTF-8");
  90. data += "&" + URLEncoder.encode("priezvisko", "UTF-8") + "=" +
  91. URLEncoder.encode(priezvisko, "UTF-8");
  92.  
  93. data += "&" + URLEncoder.encode("ulica", "UTF-8") + "=" +
  94. URLEncoder.encode(ulica, "UTF-8");
  95.  
  96. data += "&" + URLEncoder.encode("psc", "UTF-8") + "=" +
  97. URLEncoder.encode(psc, "UTF-8");
  98.  
  99. data += "&" + URLEncoder.encode("mesto", "UTF-8") + "=" +
  100. URLEncoder.encode(mesto, "UTF-8");
  101.  
  102. data += "&" + URLEncoder.encode("email", "UTF-8") + "=" +
  103. URLEncoder.encode(email, "UTF-8");
  104.  
  105. data += "&" + URLEncoder.encode("telefonne_cislo", "UTF-8") + "=" +
  106. URLEncoder.encode(telefonneCislo, "UTF-8");
  107.  
  108. // zapis do streamu
  109. output.write(data.getBytes("UTF-8"));
  110. output.flush(); // odoslanie
  111. output.close();
  112. break;
  113. }
  114.  
  115. // spristupnenie odpovedacieho (response) streamu
  116. InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());
  117. // vytvorenie readera
  118. BufferedReader reader = new BufferedReader(streamReader);
  119. // citanie udajov zo streamu
  120. while ((riadok = reader.readLine()) != null) {
  121. result += riadok;
  122. }
  123. // close
  124. reader.close();
  125. streamReader.close();
  126. } catch (Exception e) {
  127. Log.d("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", e.toString());
  128. result = null;
  129. }
  130. return result;
  131. }
  132.  
  133. protected void onPostExecute(String result) {
  134.  
  135. Toast.makeText(LocationsFoundActivity.this, "OK", Toast.LENGTH_SHORT).show();
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement