HttpURLConnection Android + Msyql

2

The code works perfectly but I need it without using AsyncTask because I call my code several times each time it finishes executing (onPostExecute) I call different methods or functions ... I need something more generic ... Can someone help me

class AsyncEtapa extends AsyncTask<String, String, String> {
    HttpURLConnection conn;
    URL url = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            url = new URL("http://192.168.0.102/sistema/" + params[0] + params[1]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "exception";
        }
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");

            conn.setDoInput(true);
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.flush();
            writer.close();
            os.close();
            conn.connect();
        } catch (IOException e1) {
            e1.printStackTrace();
            return "exception";
        }
        try {
            int response_code = conn.getResponseCode();

            if (response_code == HttpURLConnection.HTTP_OK) {

                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
                System.out.println(result.toString());
                try {
                    JSONArray jsonArray = new JSONArray(result.toString());
                    for (int i = 0; i < jsonArray.length(); i++) {
                        etapa.add(jsonArray.getJSONObject(i).getString("etapa"));
                        idEtapa.add(jsonArray.getJSONObject(i).getString("idetapa"));
                    }
                } catch (JSONException e) {
                    System.out.println("deu erro");
                }
            } else {
                return ("unsuccessful");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "exception";
        } catch (Exception e3) {
            e3.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return "Qwqweqwe";
    }

    @Override
    protected void onPostExecute(String result) {
        spn2.setAdapter(new ArrayAdapter<String>(ConfigActivity.this, android.R.layout.simple_spinner_dropdown_item, etapa));
    }
    
asked by anonymous 12.12.2017 / 12:39

1 answer

1

Try to make this class abstract and implement 2 abstract methods, so anywhere in your code this class is instantiated you can do a different programming.

Example:

abstract class AsyncEtapa extends AsyncTask<String, String, String> {
    HttpURLConnection conn;
    URL url = null;

    public abstract void preExecute();
    public abstract void postExecute();

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        this.preExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            url = new URL("http://192.168.0.102/sistema/" + params[0] + params[1]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "exception";
        }
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");

            conn.setDoInput(true);
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.flush();
            writer.close();
            os.close();
            conn.connect();
        } catch (IOException e1) {
            e1.printStackTrace();
            return "exception";
        }
        try {
            int response_code = conn.getResponseCode();

            if (response_code == HttpURLConnection.HTTP_OK) {

                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
                System.out.println(result.toString());
                try {
                    JSONArray jsonArray = new JSONArray(result.toString());
                    for (int i = 0; i < jsonArray.length(); i++) {
                        etapa.add(jsonArray.getJSONObject(i).getString("etapa"));
                        idEtapa.add(jsonArray.getJSONObject(i).getString("idetapa"));
                    }
                } catch (JSONException e) {
                    System.out.println("deu erro");
                }
            } else {
                return ("unsuccessful");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "exception";
        } catch (Exception e3) {
            e3.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return "Qwqweqwe";
    }

    @Override
    protected void onPostExecute(String result) {
        spn2.setAdapter(new ArrayAdapter<String>(ConfigActivity.this, android.R.layout.simple_spinner_dropdown_item, etapa));
        this.postExecute();
    }

Deployment:

 new AsyncEtapa() {
    @Override
    public void preExecute() {
        //Seu código customizado aqui
    }

    @Override
    public void postExecute() {
        //Seu código customizado aqui
    }
}.execute();
    
12.12.2017 / 13:51