如何处理HTTP响应InputStream?

时间:2019-06-08 17:35:36

标签: android http error-handling inputstream httpresponse

我的Android Mobile Java代码通过Http将交易记录推送到Cloud。我正在阅读Http响应,并根据响应采取进一步的措施。一切正常,但突然由于此行,应用程序开始挂起并停止工作:

responseInputStream =新的BufferedInputStream(httpURLConnection.getInputStream());

晚上又重新开始工作。所以我猜服务器上有一些问题。但是我想确保如果再次发生这种情况,我的应用程序应该不会挂起。我正在进行Http调用,并在try / catch块中读取响应,并处理所有异常,我还包括Exception,这样我就不会错过任何东西。但App仍然挂起。请任何人建议如何最好地处理阅读反应吗?

发布整个功能代码。

谢谢。

private void WebServiceConnection(String xmlString,String empid,Stringpunchdate){

    Log.e(TAG, "Inside webservice connection");
    InputStream responseInputStream = null;
    OutputStream requestOutputStream = null;

    HttpURLConnection httpURLConnection = null;

    try {

        //Server URL
        URL url = new URL("http://xyz:8084/RTTS/TransData");

        // Set the HTTP URL connection
        httpURLConnection = (HttpURLConnection)url.openConnection();

        Log.e(TAG, "http connection opened");

        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setConnectTimeout(15000);
        httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type", "text/xml");

        Log.e(TAG, "Send request");

        // Send request
        requestOutputStream = httpURLConnection.getOutputStream();
        requestOutputStream.write(xmlString.getBytes("UTF-8"));

        Log.e(TAG, "after send request");

    /*    String body = xmlString;
        OutputStream output = new BufferedOutputStream(httpURLConnection.getOutputStream());
        output.write(body.getBytes());
        output.flush();
    */

        //Receive response
        responseInputStream = new BufferedInputStream(httpURLConnection.getInputStream());

      //  responseInputStream = null;

        if(responseInputStream !=null) {
            Log.e("PushDataService", responseInputStream.toString());

            //Delete from Local
            PunchResponse punchResponse = new PunchResponse();

            ParseResponse parseResponse = new ParseResponse();

            punchResponse = parseResponse.getResponse(responseInputStream);

            Log.e("PushDataService", "Deleting record. Emp id: " + punchResponse.empid);

            if (punchResponse != null && punchResponse.empid != null) {
                Log.e("PushDataService", "Inside if response not null");
                deleteRecordLocal(punchResponse);
            } else {
                Log.e(TAG, "Null response from Web Service. REcord not found to delete");
            }

        }else {
            Log.e(TAG, "Null response returned from Web server");
        }
    }
    catch (UnknownHostException e) {
        e.printStackTrace();

    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch(SocketTimeoutException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {

        if(httpURLConnection != null) {
            httpURLConnection.disconnect();
        }

        //close input
        if (responseInputStream != null) {
            try {
                responseInputStream.close();
            } catch (IOException ioex) {
                ioex.printStackTrace();
            }
        }
        //Close output
        if (requestOutputStream != null) {
            try {
                requestOutputStream.close();
            } catch (IOException ioex) {
                ioex.printStackTrace();
            }
        }
    }
}

0 个答案:

没有答案