×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    J2me MIDlet to Servlet HttpPost connection Exception java.io.EOFException

    Hello friends,
    I am trying for for HttpPost connection.When I writing bites from MIDlet to servlet,servlet processing the request and obtain the string content that I have forward that is ok.
    But when same bites servlet send to MIDlet, then from that bites I don't able to get actual String content . I sort out the matter by bite by bite discussion ,that the bites that I am receiving from Servlet to MIDlet is totaly different that I am actually sending by servlet .
    I am writing portion of code:
    MIDlet code:
    //Sending
    HttpConnection hc;
    InputStream is;
    DataOutputStream dos;
    hc=(HttpConnection)Connector.open(addr);
    hc.setRequestMethod(HttpConnection.POST);
    dos=(DataOutputStream)hc.openDataOutputStream();
    byte[] arr=emp.persist(); //persist is method that return byte array by wring to string
    dos.write(arr,0,arr.length);
    dos.flush();
    dos.close();
    //Reading
    is=hc.openInputStream();
    DataInputStream din =new DataInputStream(is);


    System.out.println(din.readUTF()+din.readUTF());

    //Servlet code


    public void doPost(HttpServletRequest req ,HttpServletResponse res) throws ServletException,IOException
    {
    DataInputStream dis= new DataInputStream(req.getInputStream());
    String str1=dis.readUTF();
    String str2=dis.readUTF();
    dis.close();
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    DataOutputStream dos=new DataOutputStream(baos);
    dos.writeUTF(str1);
    dos.writeUTF(str2);
    dos.flush();
    dos.close();
    byte[] b=baos.toByteArray();
    res.setContentType( "application/octet-stream");
    ServletOutputStream so=res.getOutputStream();
    so.write(b);
    so.close();
    }

    Midlet send byte array:
    Text abcdefgh12345678) Bites:>0 8 97 98 99 100 101 102 103 104 0 8 49 50 51 52 53 54 55 56
    Servlet Receive Byte arrayabcdefgh12345678) Bites:>0 8 97 98 99 100 101 102 103 104 0 8 49 50 51 52 53 54 55 56

    But sevlet read same string that I have send from MIDlet But when servlet send same string it returns to midlet this bite array:

    bites:97 98 99 100 101 102 103 104 13 10 49 50 51 52 53 54 55 56 13 10

    Question:When sending string from MIDlet to servlet why 0 8 bit is coming before string is starting? Here I am sending two string.Though it does create problem for servlet,servlet receive and parse string properly.
    Why in case of servlet that send same two string parsed by servlet, 13 10( means linefeed & carraige)are coming after ending each string ?Actually these linefeed & carraige create problem for midlet to parse properly.How can I resolved this problem..Please help me around this.

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    65

    Re: J2me MIDlet to Servlet HttpPost connection Exception java.io.EOFException

    Hi,

    I am using Nokia 2630. i m getting error Exception like java.io.EOFExceptio
    when i m getting array of string. it is working gud when the servelate return only 1 string element

    where as

    NOTE: my total code is working gud on sonyericssion application. total application wihout any error

    here is the code snipet

    some one has written that it is bcz of input-stream and readUTF function - i don't know what is the problem



    Code:
    private Object invokeServer(int requestID, Object[] parameters) throws IOException {
        
    	//.....
    	// code omited 
    	// ....
    
            int response;
            try {
                response = connection.getResponseCode();
            } catch (IOException e) {
                throw new IOException("No response from " + serverURL);
            }
            if (response != 200) {
                throw new IOException(response + " " + connection.getResponseMessage());
            }
            DataInputStream input = connection.openDataInputStream();
            String sc = connection.getHeaderField("set-cookie");
            if (sc != null) {
                sessionCookie = sc;
            }
            short errorCode = input.readShort();
            if (errorCode != 1) {
                // there was a remote exception
                throw new IOException((String) readObject(input));
            }
    
            Object returnValue = readObject(input);
    
            input.close();
            connection.close();
            return returnValue;
        }
    
    
    
    private static Object readObject(DataInput in) throws IOException {
            int type = in.readShort();
            int length;
            switch (type) {
                case 7 /* STRING_TYPE */:
                    return in.readUTF();
                case 2 /* BYTE_TYPE */:
                    return new Byte(in.readByte());
                case -2: { /* ARRAY */
                    short elementType = in.readShort();
                    length = in.readInt();
                    switch (elementType) {
                        case 2 /* BYTE_TYPE */: {
                            byte[] data = new byte[length];
                            in.readFully(data);
                            return data;
                        }
                        case 7 /* STRING_TYPE */: {
                            String[] data = new String[length];
                            for (int i = 0; i < length; i++) {
                                data[i] = (String) readObject(in);
                            }
                            return data;
                        }
                    }
                }
                case -1: /* NULL */
                    return null
            }
            throw new IllegalArgumentException("Unsupported return type (" + type + ")");
        }

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    Re: J2me MIDlet to Servlet HttpPost connection Exception java.io.EOFException

    The EOFException probably is caused by using the DataStream's *UTF methods which encode the data using some Sun's bastard-implementation of unicode. An EOFException gets thrown on non-sun-UTF data when readUTF can't decode it.

    My suggestion is to use following methods when sending strings over network:

    Writing (in client in this case):
    Code:
    String encoding = getCommonEncoding();
    String message = "foobar";
    
    // No DataOutputStream needed here
    outputStream.write(message.getBytes(message, encoding);
    and for reading (in server in this case):
    Code:
    String encoding = getCommonEncoding();
    
    // This should in reality read all incoming data
    data[] buffer = new buffer[BUF_LEN];
    int bytes = inputStream.read(data);
    String message = new String(buffer, 0, bytes, encoding);
    Where getCommonEncoding negotiates a charset that's supported on both server and client side (and supports all of the characters that are used).

  4. #4
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    65

    Re: J2me MIDlet to Servlet HttpPost connection Exception java.io.EOFException

    thankx for ur valueable response

    it is very dificult for me to code getCommonEncoding(); will plz guide me in this regard.

    regards

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    Re: J2me MIDlet to Servlet HttpPost connection Exception java.io.EOFException

    Quote Originally Posted by kmsoomro View Post
    it is very dificult for me to code getCommonEncoding(); will plz guide me in this regard.
    Well, for the midlet you can get the default encoding using System.getProperty("microedition.encoding"); which (according to Sun) defaults to ISO8859-1. It's probably easier to make the server conform to the client.

    Also you can just leave the encoding-argument out of the function calls, when the string gets encoded using the default encoding, but here's a bit of a risk that some characters don't display correctly.

  6. #6
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    65

    Re: J2me MIDlet to Servlet HttpPost connection Exception java.io.EOFException

    in my application there three methods

    1. will return single string (which is working gud and i m usign readUTF() of DataInputStream)
    Code:
    [Method 1 response]
    single return: 6V5tiRKxt+oM/dS5f0LnIw==
    2. will return array of strings (not working i think bcz of array on nokia 2630 where as the same code working on sonyericsson mobile phone and sun sdk)
    Code:
    [Method 2 response]
    array in response [array of string 
    [SvB0/lqnpjR/8I/9Y8dMU94JYwCWrwXXTaFhtd7E7OJsI74ppH3KDBJ6Mu9U09NQyaBNyw57PRyNI/+om6Af+JIToFAnEEDmPoc6DJvk4DRvuDl6U7KROvtQR2qbR5kD4nCURbE4aSYvw8KvtVl78jqHAcFSyKFFG4y903bN+/ceKmJvRIVIJesQQVAj3B5asGfL/JznrSOZ/5Vu6XOCPRGrjYcTgHuJ5jfwSqvXYdsG//wmAJ05zyHUS1NyKFaYM9Ornypd4KIYcY2+STlFwvUPtFXOBpJTj1tEvkV8bj4=]
    [zrQUZFov36bMEFORTLsI/A==]
    [2iz9m9t2bw0OM8nfl7BG0e0kO6vtIUEqTg1A90RP+Ag=]
    [AD9ebIFuVLCHSoqH4Cekp+ULJGRHnv3PwaOo6BlBTdSvqHO7ntBdecCmb0QtycMiHGZnExjNxe9TNA/L0tkH23G2nrqh1jamM+fVhgvSIAaboR47UWi38UJA9X/4mlNtqoZ9CTS1F93rFzYUGv+a/lre5/0TJTQWdvj9RxAxrNDirrGbZeoeBmfjH/pcnAwiI26hESCrXfUp1n3t2gCzCJSR25dC3LXTeuEUh8edmeUrf2+7wDZvLmwQADomFURcP4BoF0+xUp3o/8p33cm/NkS6V8rHqC2lJ4nCwW5U13g=]
    [HsA3ZWR1l8AGgQ/ar3cVWw==]


    3. will return array of byes ( not working on phone it has alos same problem like the above one array has)


    i don't this that there is any wrong data inside or any espeical charaters

    then why i m getting EOFException on 2630 nokia


    i m using netbean mobility pack: servelate stub for mobile phone.

    i don't know whats wrong with this code

    in both array of datainputstream i m getting the same exception like EOFException

    regards
    Last edited by kmsoomro; 2008-01-11 at 13:02.

Similar Threads

  1. Receiving SMS by J2ME Midlet on 3410
    By mlamb in forum Mobile Java General
    Replies: 6
    Last Post: 2009-03-24, 13:13
  2. how to check gprs connection +j2me?
    By mrrajesh_1982 in forum Mobile Java General
    Replies: 1
    Last Post: 2006-10-25, 10:23
  3. Successfully Signed A Midlet but Bluetooth connection failed
    By ManiDCI in forum Mobile Java General
    Replies: 0
    Last Post: 2005-02-16, 18:17
  4. 6600: MIDlet activation on inbound connection - URGENT HELP NEEDED PLZ!
    By alatto in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2004-02-26, 12:48
  5. 7650: J2ME GPRS connection dropped
    By alexterzis in forum Mobile Java General
    Replies: 0
    Last Post: 2002-10-30, 05:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •