使用侦听器从 httpURLConnection 检索数据

Retrieving data from httpURLConnection with a listener

我想从 httpUrlConnection 检索数据并将其发送到通信处理程序 class。我创建了一个监听器。我想通过监听器将数据发送到通信处理程序 class。我该怎么做?当我想创建一个 connectiohttp 对象时,它返回一个错误。 我应该更改侦听器还是更改 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);


                }
            });
        }

尝试使用 Volley,使用它进行 http 请求很简单: https://developer.android.com/training/volley