HttpConnection在30秒后发送另一个请求

时间:2012-05-03 14:29:07

标签: java http java-me j2mepolish nokia-s40

我正在写一个应用程序,我正在向服务器发送post请求。问题是诺基亚S40系列手机如果在30秒内没有得到响应就发送2个请求。我在我的应用程序中使用s60系列手机检查了相同的工作正常。

我找到的一个解决方案是在打开连接时启动计时器,如果连接没有得到响应,则在25秒后终止连接。但它也毫无价值。在这里我附上我的代码。如果可能的话,请检查并帮助我。

                try
                { 

                          param="function=CloseRecharge&LoginId="+SharedVariable.getUserInfo().getLoginID()
                                +"&BatchId="+SharedVariable.getSelectedProduct().getBatchID()
                                +"&SystemServiceID="+SharedVariable.getSelectedProduct().getSystemServiceID()
                                +"&ReferalNumber="+strMobileNo
                                +"&FromANI="+fromMoNo
                                +"&Email="+""
                                +"&Checksum="+Checksum;

                          connection = (HttpConnection) Connector.open(url);

                            timeout=0;
                            t.schedule(new TimerTask()
                            {
                                public void run()
                                {
                                   try
                                   {
                                       timeout+=1;
                                       System.out.println("Timeout value:"+timeout);

                                       if(timeout>=25)
                                       {
                                           //timeout every 1 munite
                                            timeout=0;
                                            System.out.print("Connection is closing");

                                                connection.close();
                                                connection=null;
                                                if(is!=null)
                                                    is.close();

                                               Alert alert=new Alert("HttpConn Closed", "connection closed",null, AlertType.WARNING);
                                               alert.setTimeout(Alert.FOREVER);

                                                display.setCurrent(alert,new MainMenu("Menu", parent));
                                                this.cancel();

                                       }
                                   }
                                   catch(Exception e)
                                   {
                                       System.out.println("========Exception occured in Timer Task");
                                       e.printStackTrace();
                                   } 
                                }
                            }
                        , 0, 1000);

                             connection.setRequestMethod(HttpConnection.POST);
                             connection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
                             connection.setRequestProperty("Accept_Language","en-US");
                             connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                             connection.setRequestProperty("Content-length", ""+param.getBytes().length);
                             out = connection.openOutputStream();
                             out.write(param.getBytes());
                             out.flush();
                             param=null;
                             if (connection.getResponseCode() == HttpConnection.HTTP_OK )
                             {
                               t.cancel();
                               int len = (int)connection.getLength();
                               InputStream istrm = connection.openInputStream();
                                if (istrm == null)
                                {
                                  throw new IOException("Cannot open HTTP InputStream, aborting");
                                }
                               if (len != -1)
                               {
                                  data = new byte[len];
                                  int bytesRead = istrm.read(data);
                               }
                               else
                               {
                                  ByteArrayOutputStream bo = new ByteArrayOutputStream();
                                  int ch;
                                  int count = 0;

                                  while ((ch = istrm.read()) != -1)
                                  {
                                    bo.write(ch);
                                    count++;
                                  }
                                  data = bo.toByteArray();
                                  bo.close();
                                }
                                String response = new String(data);
        displayResponse();
                             }//if
                             else
                             {
                                 Alert error=new Alert("ResponseCode:"+connection.getResponseCode(),
                                                        null,
                                                        null,AlertType.INFO);
                                  error.setTimeout(Alert.FOREVER);
                                  display.setCurrent(error,new MainMenu("Menu", parent));
                             }
                catch(Exception exception)
                {
                    displayErrorMessage(exception); 
                }
                finally
                {
                    try
                    {
                        if(connection!=null)
                            connection.close();
                        if(is!=null)
                            is.close();
                    }
                    catch (IOException ex)
                    {
                        System.out.println("Exception in Finally block....");
                        ex.printStackTrace();
                    }
                }

0 个答案:

没有答案