Using Http connection from iOS client gives error on server.

131 views
Skip to first unread message

nir...@nvsoft.com

unread,
Mar 16, 2012, 8:39:40 AM3/16/12
to codenameone...@googlegroups.com
Hi,

I am using the following code to connect to the server using the iOS port on iPhone:

            ConnectionRequest actReq = new ConnectionRequest() {
                protected void buildRequestBody(OutputStream os) throws IOException {
                    super.buildRequestBody(os);
                    // Write the JSON object as UTF string
                    DataOutputStream dos = new DataOutputStream(os);
                    dos.writeUTF(requestObj.toString());
                    dos.flush();
                }
            };
            // Allow duplicate network connection request, as we will always
            // send the request to the same URL however with different form data
            actReq.setDuplicateSupported(true);
            actReq.setUrl(getServerURL());
            actReq.setSilentRetryCount(1); // Silently retry one more time before giving error
            actReq.setPost(true);
            actReq.setContentType("application/json");
            actReq.addRequestHeader("Accept", "application/json");
            actReq.addRequestHeader("CLIENT_API_VERSION","1.0");

            NetworkManager.getInstance().addToQueue(actReq);

And on the server I use the following code to read the request(where req is the HttpServletRequest):

          DataInputStream in = new DataInputStream(req.getInputStream());

          HttpSession session = req.getSession(true);

          String versionString = req.getHeader("CLIENT_API_VERSION");

          String inStr = in.readUTF();

This code works in codename1 simulator and correctly connects to the server and gets the response back.

However when I use the same code, for iOS port, on actual iPhone device, it connects to the server, but then on the server side it gives the following error:

java.io.EOFException

at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)

at java.io.DataInputStream.readUTF(DataInputStream.java:572)

at java.io.DataInputStream.readUTF(DataInputStream.java:547)

Also the versionString(CLIENT_API_VERSION) request header is coming out to be null when request is from actual device.

Has anyone encountered the similar issue or connected to server using the iOS port? Please let me know, if anyone has any idea what could be the issue here.

Thanks,
Nirmal

Shai Almog

unread,
Mar 16, 2012, 8:44:12 AM3/16/12
to codenameone...@googlegroups.com
Glancing at the code you probably shouldn't flush() however if you have a runnable test case I please file an issue and I'll work on getting it to work in a portable way.

Thanks.

nir...@nvsoft.com

unread,
Mar 16, 2012, 8:56:20 AM3/16/12
to codenameone...@googlegroups.com
Hi Shai,

I am sorry, I did not understand what you mean by runnable test case. This code is part of my full project, but all code related to the  connection logic is separated above. However for someone else to test this it will require  a server side code to read the request.

The test case is re-producible, in that every time I can run my code and it gives the same error on server.

Thanks,
Nirmal

nir...@nvsoft.com

unread,
Mar 16, 2012, 9:26:56 AM3/16/12
to codenameone...@googlegroups.com
Hi Shai,

Further, I also tried with a version of the code, where I am not calling the flush(). However, it still gave the same error on server.

Thanks,
Nirmal

Shai Almog

unread,
Mar 16, 2012, 12:54:31 PM3/16/12
to codenameone...@googlegroups.com
Thanks for submitting the bug. What I want is a way to reproduce this so I can test that the fix worked and debug the occurrence. If you can create client code I can run and debug (ideally connecting against a dummy server that will return whether it got the request correctly or not). That would be great.
The way things are now I need to guess what went wrong from the description and code review which might take me quite a while to do.

nir...@nvsoft.com

unread,
Mar 16, 2012, 3:05:01 PM3/16/12
to codenameone...@googlegroups.com
Hi Shai,

Please find the dropbox link to the source code for a Test project to check if the connection with iOS is working or not.


This code will connect to my server URL located at:


In case it is successful, you should see a response which produces a dialog box with the response msg. (The message will be an error msg something like, user not found). This is the success case i.e the server could read the request object correctly. You can check for this success case by running the app in netbeans simulator.

In the iPhone device, the app when run will not produce the response dialog box as the server gets an error reading the request.

I have only attached the source code (src folder), just create a new netbeans GUI builder project and replace the src folder with this files and you should be ready to test.

Hope this helps in resolving the issue.

Regards,
Nirmal 

Shai Almog

unread,
Mar 16, 2012, 3:24:58 PM3/16/12
to codenameone...@googlegroups.com
Thanks!
I'm looking into it.
Reply all
Reply to author
Forward
0 new messages