×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    188

    Exception while reading data from server.

    Hi All I amm getting exception while reading data from the server.

    Following is the code I have written to read data from server.

    private String readServerData(InputStream inputStream) throws IOException {
    StringBuffer strBuf = new StringBuffer();
    int length = (int) httpConnection.getLength();
    byte incomingData[] = null;
    if (length != -1) {
    incomingData = new byte[2048];
    while ((length = inputStream.read(incomingData)) != -1) {
    strBuf.append(new String(incomingData, 0, length, "UTF-8"));
    }
    dataBuffer = strBuf.toString();
    } else {
    ByteArrayOutputStream byteStream =
    new ByteArrayOutputStream();
    int ch;
    //reading the stream byte by byte to prevent the
    //loss of data that is sometimes observed on reading all at once.
    while ((ch = inputStream.read()) != -1) {
    byteStream.write(ch);
    }
    dataBuffer = new String(byteStream.toByteArray(), "UTF-8");
    byteStream.close();
    }
    inputStream = null;
    strBuf = null;
    incomingData = null;
    return dataBuffer;
    }

    The above code is working fine with Nokia 3110C and Nokia 2700c devices while the Exception is occuring in C5-00 though the data is same for all.

    Please help me to resolve this issue.


    Thanks & Regards,
    Ajit

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Exception while reading data from server.

    I have tested your code in order to fetch the contents of a page on C5, on a software version released on 11th December 2010 and I didn't have any problems.

    This is what I tried
    Code:
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Form;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    
    
    public class Temp extends MIDlet {
    	Form f;
    	HttpConnection httpConnection;
    	StringBuffer strBuf;
    	String dataBuffer;
    	public Temp() {
    		// TODO Auto-generated constructor stub
    	}
    
    	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    
    	}
    	protected String readServerData() {
    		try{
    			httpConnection = (HttpConnection) Connector.open("http://www.google.com");
    			InputStream inputStream=httpConnection.openInputStream();
    			strBuf = new StringBuffer();
    			int length = (int) httpConnection.getLength();
    			byte incomingData[] = null;
    			if (length != -1) {
    				incomingData = new byte[2048];
    				while ((length = inputStream.read(incomingData)) != -1) {
    					strBuf.append(new String(incomingData, 0, length, "UTF-8"));
    				}
    				dataBuffer = strBuf.toString();
    			} else 
    			{
    				ByteArrayOutputStream byteStream =
    					new ByteArrayOutputStream();
    				int ch;
    				//reading the stream byte by byte to prevent the
    				//loss of data that is sometimes observed on reading all at once.
    				while ((ch = inputStream.read()) != -1) {
    					byteStream.write(ch);
    				}
    				dataBuffer = new String(byteStream.toByteArray(), "UTF-8");
    				byteStream.close();
    			}
    			inputStream = null;
    			strBuf = null;
    			incomingData = null;
    			return dataBuffer;
    		}catch(Exception e){return null;}
    	}
    	
    	protected void pauseApp() {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    		String output;
    		output=readServerData();
    		f=new Form("");
    		f.append(output);
    		Display.getDisplay(this).setCurrent(f);
    		
    	}
    
    }
    Can you try the full example as I post it here? Can you get the message of the Exception? You can see what software version you are testing on by pressing *#0000# and update your device's software, if necessary.
    Last edited by skalogir; 2011-11-02 at 08:07.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    188

    Re: Exception while reading data from server.

    Actually the Runtime IOException is occurring when there is Chinese text coming from the server.
    With english text it is working fine.
    I am trying with around 75 KB of data which is a combination of both English and Chinese text.

    Please find the device software details as follows:

    The software version is - 062.001
    SW version date- 11-Dec-2010
    Custom version- 062.001.c00.01
    Custom version date-11-Dec-2010
    Language set- 21.01
    Model- C5-00
    Type- RM-645
    Latest update- Not updated

    What could be the root cause? Please help me...

    Thanks & Regards,
    Ajit
    Last edited by ajitkumar26; 2011-11-02 at 04:10.

  4. #4
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Exception while reading data from server.

    It's a character encoding issue. I can retrieve data, when I replace all instances of the String initialization

    Code:
    new String(byteStream.toByteArray(), "UTF-8");
    with this
    Code:
    new String(byteStream.toByteArray());
    I have tested the code with information from this page:
    http://zhengwu.beijing.gov.cn/bmfu/bmts/t1202056.htm

    You might need to handle the information you are retrieving according to the page's character encoding standard.
    For more information on chinese character encoding:
    http://en.wikipedia.org/wiki/Chinese_character_encoding

    You can see the character encoding of the page you are trying to retrieve in Windows 7 and Firefox by right clicking on the page, and selecting "View Page Source". In the page's source, you should see something like this:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    188

    Re: Exception while reading data from server.

    Thanks for your response.
    Which of the following line of code is correct (1) or (2)
    1. new String(byteStream.toByteArray(), "UTF-8");
    2.new String(byteStream.toByteArray());

    And one more thing is that the data are not only chinese or english it's a combination of both. So UTF-8 should work fine.
    Please help me...

    Thanks & Regards,
    Ajit

  6. #6
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Exception while reading data from server.

    No 2 is the code that can retrieve data from a chinese web site on your C5 device. No1 is what you provided on the code sample you posted here.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    188

    Re: Exception while reading data from server.

    Server is sending data in UTF-8 encoding format so I am using UTF-8 only
    In which encoding for both the chinese and english data can be encoded?


    Thanks & Regards,
    Ajit

  8. #8
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Exception while reading data from server.

    Have you modified the code, according to my previous post? If so, what is the result? Can you retrieve data, or do you still get the exception error? If the problem still insists, can you provide the page link?

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    188

    Re: Exception while reading data from server.

    No after changing with the cde it is working fine but while displaying sometime it might come as a junk character while displaying on the String if I will not give any encoding format. Isn't it?


    Thanks & regards,
    Ajit

  10. #10
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Exception while reading data from server.

    Hi Ajit,

    UTF-8 should work fine when a combination of Chinese and English is used in a page. You should use the same encoding as the encoding the server you are retrieving from, is using. Provided that you are retrieving data from a UTF-8 encoded page, your original code without any modifications, should work fine (i.e. the code you provided on your first post in this thread). I have tested your code, in an effort to retrieve data from this page:
    http://www.columbia.edu/~fdc/utf8/
    which is written in a combination of Chinese, English and other non-latin languages and is encoded in UTF-8.
    Your code works fine on C5-00. I cannot reproduce your error.
    Last edited by skalogir; 2011-11-04 at 11:03.

Similar Threads

  1. CActive object and server data reading
    By digitalsol in forum Symbian
    Replies: 2
    Last Post: 2010-05-15, 15:02
  2. Nokia S40 series- Http data reading Exception
    By pollux.java in forum Mobile Java Tools & SDKs
    Replies: 1
    Last Post: 2008-06-25, 09:33
  3. Reading data from server in .text file on S40 2nd Ed
    By iworktech_kiran in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2008-02-11, 09:55
  4. Replies: 6
    Last Post: 2007-01-09, 06:04
  5. Reading text data from server
    By yazwas in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2006-09-07, 17:32

Posting Permissions

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