Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.cargoalgps;
- import android.app.ProgressDialog;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class addCompany extends AppCompatActivity {
- TextView text; //wyslano/nie wyslano email pomyslnie
- EditText email; //textbox z mailem
- ProgressDialog pd; //pop up z wysylanie wiadomosci
- Button button2; //przycisk rejestruj
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_add_company); //tutaj zmien activity_register na nazwe klasy layoutu
- //jak wyzej
- button2 = (Button) findViewById(R.id.buttonCheckEmail);
- text = findViewById(R.id.textCorrectOrNot);
- email = findViewById(R.id.inputEmail);
- setTitle("Dodawanie firmy");
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Pattern pattern = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");
- Matcher mat = pattern.matcher(email.getText());
- if(mat.matches()){
- new JsonTask().execute("http://cargoalgps.cba.pl/api/rejestracja/create.php");
- }else{
- text.setText("Podano adres email w zlym formacie");
- }
- }
- });
- }
- private class JsonTask extends AsyncTask<String, String, String> {
- protected void onPreExecute() {
- super.onPreExecute();
- pd = new ProgressDialog(addCompany.this);
- pd.setMessage("Wysylanie wiadomosci...");
- pd.setCancelable(false);
- pd.show();
- }
- protected String doInBackground(String... params) {
- HttpURLConnection conn = null;
- BufferedReader reader = null;
- try {
- URL url = new URL(params[0]);
- conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
- conn.setRequestProperty("Accept", "application/json");
- conn.setDoOutput(true);
- conn.setDoInput(true);
- JSONObject jsonParam = new JSONObject();
- jsonParam.put("email", email.getText());
- jsonParam.put("ranga", "wlasciciel");
- jsonParam.put("wlasciciel", "self");
- Log.i("JSON", jsonParam.toString());
- DataOutputStream os = new DataOutputStream(conn.getOutputStream());
- //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
- os.writeBytes(jsonParam.toString());
- os.flush();
- os.close();
- if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
- reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- } else {
- reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
- }
- StringBuffer buffer = new StringBuffer();
- String line = "";
- while ((line = reader.readLine()) != null) {
- buffer.append(line + "\n");
- Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
- }
- return String.valueOf(conn.getResponseCode());
- // return buffer.toString();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- } finally {
- if (conn != null) {
- conn.disconnect();
- }
- try {
- if (reader != null) {
- reader.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- if (pd.isShowing()) {
- pd.dismiss();
- }
- if (result.equals("200")) {
- text.setText("Wyslano email pomyslnie");
- } else {
- String error = "";
- if(result.equals("409")){
- error = "Email juz istnieje";
- }else{
- error = "Bledny email";
- }
- text.setText(error);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement