English 中文(简体)
J2ME HttpConnection getResponseCode() blocks waiting answer from server on Nokia N97
原标题:

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("
".getBytes());
       os.write("--".getBytes());

       os.write(boundary.getBytes());

       os.write("--".getBytes());
       os.write("
".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

        }

}

最佳回答

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.

问题回答

暂无回答




相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

Listen to a port that is in use [duplicate]

Possible Duplicate: Get connecting IP from specified ports that using by other program. If a port is used by a program, is there any way I can listen that port and get the connected IP on that ...

Twisted Spread suitable for multiplayer racing sim?

Do you think that Twisted Spread may be suitable (in terms of performance) for a multiplayer racing simulator? The rest of the application is based on Python-Ogre. Can Perspective Broker run upon (...

Optimizing a LAN server for a game

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. ...

multicast ip address - blocked in call to recvfrom

i am writing a simple multicast application. i intend to run it on localhost. i have done the following: char *maddr; . . . sendfd = socket(...); struct sockaddr_in sasend; sasend.sin_family = ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

热门标签