HTTP获取请求 - 程序停止

时间:2016-11-15 14:26:59

标签: java http httpurlconnection

我正在开发一个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"。

编辑:每小时调制一次调制解调器。避免了问题,没有解决它。 任何人? :)

非常感谢提前。

2 个答案:

答案 0 :(得分:1)

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

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

直接在setRequestProperty();

之后

它可以解决冻结。

答案 1 :(得分:0)

原来问题是由这一行引起的

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

删除它并像魅力一样运行。 解决了。