Search code examples
javaandroidlistenerhttpurlconnection

Retrieving data from httpURLConnection with a listener


I want to retrieve data from the httpUrlConnection and send it to the communicationhandler class. I have created a listener. With the listener i want to send the data to the communicationhandler class. How can i do it? When i want to create a connectiohttp object, it gives a error back. Should i change the listener or change the connectionHttp class

public class ConnectionHttp extends AsyncTask<String, Void, String>{
        HttpListener listener;
        String server_response="";
        public ConnectionHttp(HttpListener listener) {
            this.listener = listener;
        }



        @Override
        protected String doInBackground(String... params) {
            return sendDataToServer(params);
        }

        public String sendDataToServer(String... params) {

            HttpURLConnection httpURLConnection = null;
            try {
                // make a httpURLconnection to the message-handler with the given httpaddress
                httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
                // set the method to POST
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                // send the given data to the message-handler
                DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
                wr.writeBytes(params[1]);
                wr.flush();
                wr.close();
                //retrieve a response from the message-handler
                InputStream inputStream;
                // retrieve the status of the HTTPURLConnection
                int status = httpURLConnection.getResponseCode();
                // check the httpURLConnection
                if (status != HttpURLConnection.HTTP_OK)
                    inputStream = httpURLConnection.getErrorStream();
                else
                //    listener.getHTTPResponse(readStream( httpURLConnection.getInputStream()));
                    server_response = readStream( httpURLConnection.getInputStream());
               // screenActivity.retrieveMessage(server_response);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
            }
            return null;

        }
        private String readStream(InputStream in) {
            String screenData= "";
            BufferedReader reader = null;
            StringBuffer response = new StringBuffer();
            try {
                reader = new BufferedReader(new InputStreamReader(in));
                String line = "";
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return response.toString();
        }

        protected void onPostExecute(String result) {
            listener.onResultReceived(result);
        }
        public interface HttpListener {
            void onResultReceived(String result);
        }


    }

    public class CommunicationHandler extends AppCompatActivity implements ConnectionHttp.HttpListener {
        JsonUtil jsonUtil;
        private String httpAdress = "http://10.0.2.2:1234/MessageHandler/ConnectionHttpServlet";


        public void sendConfigurationHandscanner() {
            //check if the handscanner has internetconnection
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // fill the PtConfig object
                    jsonUtil = new JsonUtil();
                    String send = jsonUtil.toJSONConfig();
                    // send the json object to the message-handler
                    new ConnectionHttp(this).execute(httpAdress, send);


                }
            });
        }

Solution

  • try to use Volley, it's simple to do http request with that : https://developer.android.com/training/volley