J2ME HttpConnection getResponseCode() 阻止诺基亚 N97 上的服务器等待应答

发布于 2024-08-14 05:49:52 字数 1803 浏览 4 评论 0原文

我从诺基亚 N97 手机上传文件到服务器,一切正常,但文件上传后我想从服务器获得响应。问题是我在半分钟或更长时间后才得到回复。据我所知, httpConnection.getResponseCode() 中的代码块等待响应。我在索尼爱立信上进行了测试,并且响应速度非常快,因此我认为是诺基亚 N97 的问题。 (不是服务器问题,因为它在其他手机上工作正常)

有谁知道为什么这种情况只发生在诺基亚上?

下面是一个代码片段:

public 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();

}

public 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

        }

}

I upload a file from a Nokia N97 phone to server, everything works fine but after file is uploaded I want to get response from server. The problem is that I get response only after half a minute or more. From what I see the code blocks in httpConnection.getResponseCode() waiting for response. I tested it on a Sony Ericsson and I get response very fast so I assume is a Nokia N97 problem. (is not a server problem because it works fine on other phones)

Does anyone knows why this thing happens only on Nokia?

Here is a code snippet:

public 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();

}

public 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

        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2024-08-21 05:49:52

您只认为您的文件已上传。

对 getResponseCode() 的调用是导致 HttpConnection 实际连接到服务器的第一个调用。

在上传文件之前它不会返回,这可能需要一分钟多的时间。

根据 JSR-118 中的 HttpConnection 规范,这是完全有效的行为:


当连接处于“设置”状态时,以下方法会导致转换到“已连接”状态。

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

我预计您尝试过的其他手机不如 N97 强大,并且需要刷新 OutputStream,因此应按照规范连接到服务器。

You only think your file has been uploaded.

Your call to getResponseCode() is the first one that causes the HttpConnection to actually connect to the server.

It won't return until it uploads the file, which presumably takes more than a minute.

This is perfectly valid behavior according to the HttpConnection specification in JSR-118:

"
The following methods cause the transition to the Connected state when the connection is in Setup state.

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

"

I expect the other phones you have tried this on are less powerful than the N97 and need to flush the OutputStream, therefore connecting to the server before they should according to the specification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文