【问题标题】:Network Connection Failed after 10 minutes on blackberry在黑莓上 10 分钟后网络连接失败
【发布时间】:2013-06-20 05:13:39
【问题描述】:

我已经在后台应用程序上实现了定时器任务。

我收集了当前的经纬度。每 30 秒发送一次到服务器。

我使用下面的代码将信息发送到服务器。发送成功..

我的问题是,在我检查了 10 分钟后,我无法发送。它会引发无网络错误。我也检查了浏览器,但没有网络。

如果重置设备,它会再次正常工作。但同样的问题会在 5 或 10 分钟后出现。

如何解决?

我的代码是,

try 
{
    StreamConnection connection = (StreamConnection) Connector.open(url+suffix);
    ((HttpConnection) connection).setRequestMethod(HttpConnection.GET);
    int responseCode = ((HttpConnection) connection).getResponseCode();
    if (responseCode != HttpConnection.HTTP_OK) {
        showDialog("Unexpected response code :"+ responseCode);
        connection.close();
        return;
    }
    ((HttpConnection) connection).getHeaderField("Content-type");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream responseData = connection.openInputStream();
    byte[] buffer = new byte[1000];
    int bytesRead = responseData.read(buffer);
    while (bytesRead > 0) {
        baos.write(buffer, 0, bytesRead);
        bytesRead = responseData.read(buffer);
    }
    baos.close();
    connection.close();
    String s = new String(baos.toByteArray());
    showDialog("Responce from server "+s);
}
catch (IOException e) 
{
}

【问题讨论】:

    标签: blackberry java-me


    【解决方案1】:

    通常,当您遇到一些问题,它工作了几次,然后停止工作,并且您需要重置设备时,您所做的事情已经用尽了所有可用资源,而您没有释放它们完成。

    当执行重复的网络操作时,你应该在每次使用后清理你的流和连接。

    通常,编写网络代码的正确方法是在try 块之外声明网络变量,在try 内分配和使用它们,同时捕获任何抛出的IOExceptions。然后,您使用finally 块来清理您的资源,无论代码是否成功完成。

    我还要注意,当调试网络问题时,您不希望有一个catch() 处理程序来简单地捕获异常并且对它们不做任何事情。将消息打印到控制台(用于测试)或将错误记录到文件中。

    最后,我看不到您的 showDialog() 方法,但如果它向用户/测试人员显示 UI,您需要在 UI 线程上执行此操作。但是,您上面显示的网络代码应该在后台线程上运行,以保持 UI 响应。因此,在showDialog() 中,只需确保您使用代码在 UI 线程上修改 UI。

    所以,一个更好的实现可能是这样的:

       private void requestFromServer() {
          StreamConnection connection = null;
          ByteArrayOutputStream baos = null;
          InputStream responseData = null;
          try 
          {
             connection = (StreamConnection) Connector.open(url+suffix);
             ((HttpConnection) connection).setRequestMethod(HttpConnection.GET);
             int responseCode = ((HttpConnection) connection).getResponseCode();
             if (responseCode != HttpConnection.HTTP_OK) {
                showDialog("Unexpected response code :"+ responseCode);
                return;
             }
             ((HttpConnection) connection).getHeaderField("Content-type");
             baos = new ByteArrayOutputStream();
             responseData = connection.openInputStream();
             byte[] buffer = new byte[1000];
             int bytesRead = responseData.read(buffer);
             while (bytesRead > 0) {
                baos.write(buffer, 0, bytesRead);
                bytesRead = responseData.read(buffer);
             }         
             String s = new String(baos.toByteArray());
             showDialog("Responce from server "+s);
          }
          catch (IOException e) 
          {
             System.out.println("Network error: " + e.getMessage());
          }
          finally 
          {
             try {
                if (connection != null) {
                   connection.close();
                }
                if (baos != null) {
                   baos.close();
                }
                if (responseData != null) {
                   responseData.close();
                }            
             } catch (IOException e) {
                // nothing to do here           
             }         
          }
       }
    
       private void showDialog(final String msg) {
          UiApplication.getUiApplication().invokeLater(new Runnable() {
             public void run() {
                Dialog.alert(msg);
             }
          });
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      相关资源
      最近更新 更多