2

我在一些客户的手机上遇到了一个奇怪的问题。看来,在服务区之外几个小时后,我的 Android 应用程序将失去访问网络的能力。网络浏览器或电子邮件等其他应用程序可以访问网络,但我的应用程序不能。

唯一可以想到的解释是,在没有数据服务的情况下,它以某种方式泄漏了套接字。

这是我的代码:

String sendWebPOST(String url, String pPostData) throws IOException {
    AndroidHttpClient c = null;
    InputStream is = null;
    OutputStream os = null;
    int rc;
    String strResponse = null;

    try {            
        c = AndroidHttpClient.newInstance("Mobile");

        // Set the request method and headers
        HttpPost request = new HttpPost(url);
        request.addHeader("Content-Type", "application/x-www-form-urlencoded");

        request.setEntity(new StringEntity(pPostData));

        try {
            HttpResponse response = c.execute(request);

            // Getting the response code will open the connection,
            // send the request, and read the HTTP response headers.
            // The headers are stored until requested.
            rc = response.getStatusLine().getStatusCode();
            if (rc != HttpStatus.SC_OK) {
                throw new IOException("HTTP response code: " + rc);
            }

            try {
                is = response.getEntity().getContent();

                int ch;
                StringBuffer sb = new StringBuffer();
                while ((ch = is.read()) != -1) {
                    sb.append((char) ch);
                }
                //if(sb.length() > 0)
                strResponse = sb.toString();
            }
            finally {
                if (is != null) {
                    is.close();
                }
            }
        }
        finally {
            if (os != null) {
                os.close();
            }
        }
    }
    catch (ClassCastException e) {
        throw new IllegalArgumentException("Not an HTTP URL");
    }
    finally {
        if (c != null) {
            c.close();
        }
    }

    return strResponse;
}

此函数大约每十分钟调用一次,以向远程服务器发送更新。当应用程序进入这种奇怪的状态时,用户仍然能够打开活动、与菜单交互等,因此应用程序仍在运行。但是,它无法通过网络发送任何内容。

任何想法可能会发生什么?

有问题的手机是 myTouch 4G 和在 T-Mobile 网络上运行 Android 2.3 的三星 Galaxy II。

谢谢。

4

1 回答 1

1

出于某种原因,使用 HttpURLConnection 而不是 AndroidHttpClient 会更好。一旦我升级了我的代码,我就不再报告任何问题了。

 HttpURLConnection conn = null;
    InputStream is = null;
    OutputStream os = null;
    String strResponse = null;

    try {
        conn = (HttpURLConnection)(new URL(url)).openConnection();

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "close");
        conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
        conn.setConnectTimeout(30000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setChunkedStreamingMode(0);
        conn.connect();

        os = new BufferedOutputStream(conn.getOutputStream());

        os.write(pPostData.getBytes());
        os.flush();

        // Getting the response code will open the connection,
        // send the request, and read the HTTP response headers.
        // The headers are stored until requested.
        int rc = conn.getResponseCode();
        if (rc != HttpURLConnection.HTTP_OK) {
            throw new IOException("HTTP response code: " + rc);
        }

        is = new BufferedInputStream(conn.getInputStream());

        int ch;
        StringBuffer sb = new StringBuffer();
        while ((ch = is.read()) != -1) {
            sb.append((char) ch);
        }
        //if(sb.length() > 0)
        strResponse = sb.toString();
    }
    finally {

        if (is != null) {
            try {
                is.close();
            }
            catch (Exception ex) {
                System.out.println("is.close ex " + ex);
            }
        }
        if (os != null) {
            try {
                os.close();
            }
            catch (Exception ex) {
                System.out.println("os.close ex " + ex);
            }
        }

        if (conn != null) {
            try {
                conn.disconnect();
            }
            catch (Exception ex) {
                System.out.println("disconnect ex " + ex);
            }
        }
    }
于 2012-07-07T22:49:15.227 回答