2009-12-09 17 views
1

我從諾基亞N97手機上傳文件到服務器,一切工作正常,但文件被上傳後,我想從服務器響應。問題是我只在半分鐘或更長時間後纔得到回覆。從我看到的httpConnection.getResponseCode()中的代碼塊等待響應。我在索尼愛立信上測試過它,並且我的響應速度非常快,所以我認爲它是諾基亞N97問題。 (不是服務器的問題,因爲它工作正常,在其他手機)J2ME的HttpConnection getResponseCode()塊的諾基亞N97等待來自服務器的回答

有誰知道爲什麼這件事情只是發生在諾基亞?

這裏是一個代碼段:

公共uploadFile(){

 httpConn = (HttpsConnection) Connector.open(url, Connector.READ_WRITE); 
     ... 
     //set httpConn parameters and request method 
     ... 

     writeParametersAndFileName(os, "text/plain"); 


     **writeFileToStream(os);** 

     os.write("\r\n".getBytes()); 
     os.write("--".getBytes()); 

     os.write(boundary.getBytes()); 

     os.write("--".getBytes()); 
     os.write("\r\n".getBytes()); 

     *//here code blocks on Nokia N97. Same thing happens if I use os.flush() after 
     // I send chunks of the file* 
     ..... 
     codeResp = httpConn.getResponseCode(); 
     // check condition 
     checkResponseHeader(); 

}

公共writeFileToStream(){

 InputStream is = null; 
     FileConnection c = null; 
     try 
     { 
      c = (FileConnection) Connector.open("file:///"+ sourcePath, Connector.READ); 

      if(c.exists()) { 
       is = c.openInputStream(); 

       long fs = c.fileSize(); 
       while (total < fs) { 
        byte[] data = new byte[512]; 
        int readAmount = is.read(data,0,512);      
        total += readAmount; 
        os.write(data, 0, readAmount); 


      } 

      //os.flush(); 
     } 
     catch (IOException ex) { 
      //    
     } 
     finally 
     { 
      //close connection 

     } 

}

回答

4

你只認爲你的文件已經上傳。

您對getResponseCode()的調用是導致HttpConnection的真正連接到服務器的第一個。

它不會返回,直到它上傳文件,這大概需要一分鐘以上。

這是根據在HttpConnection的說明書完全有效的行爲JSR-118:

「 下面的方法使所述過渡到連接狀態時,連接處於安裝狀態

* openInputStream 
* openDataInputStream 
* getLength 
* getType 
* getEncoding 
* getHeaderField 
* getResponseCode 
* getResponseMessage 
* getHeaderFieldInt 
* getHeaderFieldDate 
* getExpiration 
* getDate 
* getLastModified 
* getHeaderField 
* getHeaderFieldKey 

我希望你已經試過這在其他手機比N97那麼強大,需要衝洗的OutputStream,因此連接到服務器,他們應該根據日前將e規範。

相關問題