HTTP 获取请求 - 程序停止

HTTP Get Request - Program Stops

我正在开发一个 javaME 项目,我在其中从设备读取变量并在网站中更新它。基本上,在 Eclipse 中,应用程序运行完美。但是当我的 java 调制解调器 运行(代码运行得更快)时,它会在 运行 后卡住一个小时。我做了一些测试,程序在我尝试获取响应代码的那一行停止了。那么,我是否错误地构建了方法?我应该做些不同的事情吗?

  void enviarPost(int i, String comando)throws IOException, IllegalStateException, IllegalArgumentException, ATCommandFailedException{     

        System.out.println("Connecting to websitedummy.com...");
            if(i == 1)
            {
            url = "http://websitedummy.com/index.php?IMEI=" + imeiX + "&IP=" + ipX;
            }
            //53543D303B44723D4E616F
            else
            {
            System.out.println("Atribuir url2");
            url2 = comando;             
            url = "http://websitedummy.com/index.php?data={\"IMEI\":\""+imeiX+"\",\"TS\":\"20/04/13-08:31:44\",\"SER\":\""+url2+"\"}";               

            System.out.println("Atribuiu: "+ url2);
            }



            try {
                Thread.sleep(1000);
                System.out.println("Done1");
                connection = (HttpConnection) Connector.open(url);
                System.out.println("Done2");
                Thread.sleep(500);
                connection.setRequestMethod(HttpConnection.GET);
                System.out.println("Done3");
                Thread.sleep(500);
                connection.setRequestProperty("Content-Type", "text/plain");
                System.out.println("Done4");
                Thread.sleep(500);
                connection.setRequestProperty("Connection", "close");
                System.out.println("Done5");
                Thread.sleep(500);
                int con = connection.getResponseCode();
                Thread.sleep(500);
                System.out.println(con);
                if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
                    System.out.println("Vamos6");
                    inputstream_ = connection.openInputStream();
                    int ch;
                    while ((ch = inputstream_.read()) != -1 ) {
                        dataReceived.append((char) ch);

                    }
                    System.out.println("Updated");
                    acabouatualizar=1;
                    connection.close();
                    System.out.println("Closed");
                } else {
                    System.out.println("Error");
                    // Connection not ok
                }
            } catch (Exception e) {
              System.out.println(e);

            } finally {
                if (inputstream_ != null) {
                    try {

                        inputstream_.close();

                    } catch (Exception e1) {
                        System.out.println( e1);
                    }
                }
                if (connection == null) {
                    try {
                        System.out.println("Connection closed");
                        connection.close();

                    } catch (Exception e2) {
                        System.out.println(e2);
                    }
                }
            }
            }

程序停止并且没有出现任何类型的异常,这让我认为它只是死机了。我能看到的最后一个输出是 "Done5".

编辑:使调制解调器每小时重置一次。避免了问题,而不是解决了问题。 任何人? :)

非常感谢。

为了帮助调试并让您能够知道问题是否是连接从未真正关闭,请尝试为连接设置超时:

con.setConnectTimeout(5000); //set timeout to 5 seconds

直接在 setRequestProperty() 之后;

可能会解决冻结问题。

原来问题出在这行

        connection.setRequestProperty("Connection", "close");

删除它并运行得很好。 解决了。