caparol6991

Dodawanie firmy

Apr 13th, 2019
155
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.cargoalgps;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.os.AsyncTask;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.DataOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.net.HttpURLConnection;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;
  26.  
  27. public class addCompany extends AppCompatActivity {
  28.  
  29. TextView text; //wyslano/nie wyslano email pomyslnie
  30. EditText email; //textbox z mailem
  31. ProgressDialog pd; //pop up z wysylanie wiadomosci
  32. Button button2; //przycisk rejestruj
  33.  
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_add_company); //tutaj zmien activity_register na nazwe klasy layoutu
  38.  
  39. //jak wyzej
  40. button2 = (Button) findViewById(R.id.buttonCheckEmail);
  41. text = findViewById(R.id.textCorrectOrNot);
  42. email = findViewById(R.id.inputEmail);
  43. setTitle("Dodawanie firmy");
  44.  
  45. button2.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. Pattern pattern = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");
  49. Matcher mat = pattern.matcher(email.getText());
  50.  
  51. if(mat.matches()){
  52. new JsonTask().execute("http://cargoalgps.cba.pl/api/rejestracja/create.php");
  53. }else{
  54. text.setText("Podano adres email w zlym formacie");
  55. }
  56.  
  57. }
  58. });
  59. }
  60.  
  61. private class JsonTask extends AsyncTask<String, String, String> {
  62.  
  63. protected void onPreExecute() {
  64. super.onPreExecute();
  65.  
  66. pd = new ProgressDialog(addCompany.this);
  67. pd.setMessage("Wysylanie wiadomosci...");
  68. pd.setCancelable(false);
  69. pd.show();
  70. }
  71.  
  72. protected String doInBackground(String... params) {
  73.  
  74.  
  75. HttpURLConnection conn = null;
  76. BufferedReader reader = null;
  77.  
  78. try {
  79. URL url = new URL(params[0]);
  80. conn = (HttpURLConnection) url.openConnection();
  81. conn.setRequestMethod("POST");
  82. conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  83. conn.setRequestProperty("Accept", "application/json");
  84. conn.setDoOutput(true);
  85. conn.setDoInput(true);
  86.  
  87. JSONObject jsonParam = new JSONObject();
  88. jsonParam.put("email", email.getText());
  89. jsonParam.put("ranga", "wlasciciel");
  90. jsonParam.put("wlasciciel", "self");
  91.  
  92. Log.i("JSON", jsonParam.toString());
  93. DataOutputStream os = new DataOutputStream(conn.getOutputStream());
  94. //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
  95. os.writeBytes(jsonParam.toString());
  96.  
  97. os.flush();
  98. os.close();
  99.  
  100.  
  101.  
  102. if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
  103. reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  104. } else {
  105. reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
  106. }
  107. StringBuffer buffer = new StringBuffer();
  108. String line = "";
  109.  
  110. while ((line = reader.readLine()) != null) {
  111. buffer.append(line + "\n");
  112. Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
  113.  
  114. }
  115. return String.valueOf(conn.getResponseCode());
  116. // return buffer.toString();
  117.  
  118.  
  119. } catch (MalformedURLException e) {
  120. e.printStackTrace();
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. } catch (JSONException e) {
  124. e.printStackTrace();
  125. } finally {
  126. if (conn != null) {
  127. conn.disconnect();
  128. }
  129. try {
  130. if (reader != null) {
  131. reader.close();
  132. }
  133. } catch (IOException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137.  
  138. return null;
  139. }
  140.  
  141. @Override
  142. protected void onPostExecute(String result) {
  143. super.onPostExecute(result);
  144. if (pd.isShowing()) {
  145. pd.dismiss();
  146. }
  147. if (result.equals("200")) {
  148. text.setText("Wyslano email pomyslnie");
  149. } else {
  150. String error = "";
  151. if(result.equals("409")){
  152. error = "Email juz istnieje";
  153. }else{
  154. error = "Bledny email";
  155. }
  156. text.setText(error);
  157. }
  158.  
  159. }
  160. }
  161. }
RAW Paste Data