2012-01-18 76 views
0

在三星C6112手機上,m面臨奇怪的問題。三星C6112(J2ME手機)InterruptedIOException問題

我的midlet應用程序試圖連接到HTTPS url。 (其託管在具有版本證書的IIS 6.0 Web服務器上)。

我使用POST方法發送數據。在寫完帖子數據後,我打電話outputstream.flush()outputstream.close()方法。這兩種方法給出「InterruptedIOExceptionIOException:TCP打開」。

如果我評論兩種方法,即.flush()和.close(),hc.openInputStream();拋出相同的異常。

下面的示例代碼,如果有什麼問題,請告訴我。

InputStream is = null; 
OutputStream dos = null; 
HttpConnection hc = null; 
try { 
     hc = (HttpConnection) Connector.open(url,Connector.READ_WRITE,true); 

     byte b1[] = "Hello_World".getBytes(); 

     hc.setRequestMethod(HttpConnection.POST); 

     dos = hc.openOutputStream(); 

     int i = 0; 
     while (i < b1.length) { 
      dos.write(b1[i]); 
      i++; 
     } 
     dos.write("\r\n".getBytes()); 

     dos.flush(); // gives **InterruptedIOException** or **IOException:TCP open** 
     dos.close(); // gives **InterruptedIOException** or **IOException:TCP open** 

     is = hc.openInputStream(); 

     byte b[]; 
     ByteArrayOutputStream bStrm = new ByteArrayOutputStream(); 
     int ch, downloadedData=0; 
     while ((ch = is.read()) != -1) { 
      downloadedData++; 
      bStrm.write(ch); 
     } 
     b = bStrm.toByteArray(); 

    } catch (javax.microedition.pki.CertificateException ce) { 
     System.out.println("CertificateException in " + ce.toString()); 
    } catch (SecurityException se) { 
     System.out.println("SecurityException: ", se.toString()); 
    } catch (ConnectionNotFoundException cnfe) { 
     System.out.println("ConnectionNotFoundException: ", cnfe.toString()); 
    } catch (IOException ioe) { 
     System.out.println("IOException: ", ioe.toString() + ioe.getMessage()); 
    } catch (Exception e) { 
     System.out.println("Exception: ", e.toString()); 
    } finally { 
     if (hc != null) { 
      try { 
       hc.close(); 
       hc = null; 
      } catch (Exception e) { 
       e.printStackTrace(); 
       System.out.println("finally hc.close(); IOException " + e.getMessage() + " " + e.toString()); 
      } 
     } 

     if(is !=null) { 
      try { 
       is.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       System.out.println("finally is.close(); Exception " + e.getMessage() + " " + e.toString()); 
      } 
     } 
    } 

請讓我知道如何處理三星j2me手機。

回答

0

試試這個,

代替,

dos = hc.openOutputStream(); 

試試這個,

dos = (OutputStream) hc.openOutputStream(); 

,而不是和,

is = hc.openInputStream(); 

試試這個,

is = (InputStream) hc.openInputStream(); 

但是我想建議您使用DataInputStream & DataOutputStream類爲這些操作。

+0

嘿路西法,謝謝你的回覆。我嘗試過你的解決方案,但仍然給「InterruptedIOexception」。 – Sandeep 2012-01-19 08:17:46

+0

你能上傳你的最新代碼嗎? – Lucifer 2012-01-19 08:19:27

+0

是的,您需要在is = hc.openInputStream()行之後編寫.getResponseCode()。 – Lucifer 2012-01-19 08:55:30