You Are Here:

Community: Developer Discussion Boards

#1 Old how to decode charset=utf-8 - 2004-10-07, 10:32

Join Date: Nov 2003
Posts: 112
sumn79
Offline
Regular Contributor
hi, i just create an application that post and return data using HTTPConnection via Nokia 40 series mobile, the return type is 'text/html charset=UTF-8', the value return is incorrect,
suppose return '01234567', but return something like :
[B#ea0ef881
[B@84aee8b, although the return value is '0123456789', the i will get different every time,

if i try on another model Nokia 3650
'text/html charset=GB2312', the value return is no problem

any idea? please help

thanks

this is the code :

String postViaHttpConnection(String url) throws IOException
{
Font fnt=Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL);

HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
String returnBuffer="";

setHTTPStatus("Connecting...");
try
{
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - Open");
System.out.println("postViaHttpConnection() - Start");
//setHTTPStatus("HTTP Opening...");
c = (HttpConnection)Connector.open(url);

// Set the request method and headers
//setHTTPStatus("HTTP Setup...");
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("If-Modified-Since",
"29 Oct 1999 19:43:31 GMT");
c.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");

// Getting the output stream may flush the headers
//setHTTPStatus("HTTP Output Setup...");
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - OpenWriteStream");
os = c.openOutputStream();

//setHTTPStatus("HTTP Writing...");
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - WriteStream");
os.write("LIST games\n".getBytes());
//setHTTPStatus("HTTP flushing...");
os.flush(); // Optional, openInputStream will flush

// Opening the InputStream will open the connection
// and read the HTTP headers. They are stored until
// requested.
//setHTTPStatus("HTTP Input Setup...");
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - OpenReadStream");
is = c.openInputStream();

// Get the ContentType
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - GetType()");
String type = c.getType();
//processType(type);
// Get the length and process the data
//setHTTPStatus("HTTP Reading data...");
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - Type : "+type);
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - Ret : "+c.getResponseMessage());

tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - ReadStream");
int len = (int)c.getLength();
if (len > 0)
{
/*
byte[] data = new byte[len];
int actual = is.read(data);
//process(data);
System.out.print("AtOnce : "+data);
System.out.println("");
returnBuffer=""+data;
tStringList.addToStringList(fnt,log,getWidth(),"*ReadAtOnce - "+returnBuffer);
*/
byte[] data = new byte[len];
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bais);
dis.read(data);
returnBuffer=dis.readUTF();
tStringList.addToStringList(fnt,log,getWidth(),"*ReadAtOnce - "+returnBuffer);
}
else
{
int ch;
StringBuffer strBuff=new StringBuffer();
while ((ch = is.read()) != -1)
{
if (ch!=0)
{
strBuff.append((char)ch);
}
tStringList.addToStringList(fnt,log,getWidth(),"*Read - "+ch);
//process((byte)ch);
}
System.out.println("-------------------HTTP return - Start");
System.out.println(strBuff.toString());
System.out.println("-------------------HTTP return - End");
returnBuffer=strBuff.toString();
strBuff=null;
}
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - Ret : "+returnBuffer);
}
catch (Exception e)
{
tStringList.addToStringList(fnt,log,getWidth(),"*HTTP - Exception : "+e.toString());
System.out.println("postViaHttpConnection() - Exception : "+e.toString());
}
finally
{
//setHTTPStatus("HTTP Closing...");
System.out.println("postViaHttpConnection() - finally");
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
setHTTPStatus("HTTP Done");
return returnBuffer;
}
}
Reply With Quote

#2 Old 2004-10-07, 12:08

Join Date: Oct 2004
Posts: 79
Donz
Offline
Regular Contributor
I had some problems with readUTF() method, because order of bytes in stream don't concur order of bytes as method readUTF() realize.
So I have decide to write my own method for constructing UTF-8 string from array of bytes:
Code:
		StringBuffer str = new StringBuffer();
		InputStream iStream = null;
		try
		{
//I read UTF-8 string from file in jar
			iStream = Class.forName( "java.lang.Object" ).getResourceAsStream( resourceName );
//First two bytes in UTF-8 resource are for service propose
			a = iStream.readByte();
			a = iStream.readByte();
			while( true )
			{
				a = iStream.readByte();
				b = iStream.readByte();
				str.append( ( char ) ( ( b << 8 ) | a ) );
			}
		}
		String s = str.toString()
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia