Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 33
Registered: ‎06-12-2009
My Device: Not Specified
Accepted Solution

Parsing Response of HttpConnection

I'm having a problem parsing content from a http request I'm making via HttpConnection. I'm posting variables to the server (username/password) and everything is going well. The server is returning a response as expected. The response, however, contains a lot of strange characters that I don't understand and am having trouble parsing out.

 

I'm using ASP.net handler to return:

HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.Write("success");

 

 Using fiddler (and chrome) I can see the response the server is sending is:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 04 Nov 2011 14:10:49 GMT
Content-Length: 7
success

 

However, when I debug the response of the HttpConnection on the blackberry, I get:

\u0010\b\u0001\n\u0002\u0001\u0003\t\ntext/plain\u0003\u0005\u0080\u0080\u0080\u0080j\u0007\n\u0080\u0080\u0080\u0080B\u0080\u0080\u0080\u0080\u0000\u0000\u0080\u0080\u0080\u0080\u0016\u0001\u0000\u0007success\t\u0001\u0000\u0001\u0000\u0000\b\u0000\u0000\u0007\u0002\u0000

 

I've come to understand the values starting with \u00 are "control characters", but thats about all I understand. I can parse these out by checking the byte value as I'm reading the stream with this if statement:

DataInputStream datainputstream = conn.openDataInputStream();
byte[] buffer = new byte[1];
StringBuffer sb = new StringBuffer();
int readIn = 0;
while((readIn = datainputstream.read(buffer)) > 0)
{
    String temp = new String(buffer, 0, readIn);
    if (buffer[0] > 31 && buffer[0] != 127) // remove control characters
        sb.append(temp);
}

 

Which leads me to the final problem. After parsing out these characters, I'm left with:

text/plainjBsuccess

 

The control characters I get, but where is the jB coming from? Even stranger, when I change the message from "success" to anything else, the "jB" characters change to something different, seemingly random.

 

Can anyone help me figure out what's going on here? I'm thinking/hoping there must be an easier way to do what I'm trying to accomplish. My final thought would be to wrap my entire response (success) in a set of unique characters and do a search + parse it out that way, but I feel that shouldn't be necessary.

 

Thanks in advance.

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Parsing Response of HttpConnection

isn't it sufficient for your task to check if the responsecode is 200?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 33
Registered: ‎06-12-2009
My Device: Not Specified

Re: Parsing Response of HttpConnection

Hi Simon,

 

I suppose it could be. I have also considered wrapping my response body in some special characters (### for example), but I didn't feel it should be necessary.

 

Truthfully, it bothers me that I don't understand what's going on here. I'm also sure this isn't the last time I'll have to deal with this, so I'm interested in finding an elegant solution.

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Parsing Response of HttpConnection

i would suggest to intercept the response with tcpmon, wireshark or similar, maybe using the simulator. you can compare the stuff that arrives ota with the data you read from the connection, maybe it helps.

for reading i usually take
http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/io/IOUtilities.html#streamToBy...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Parsing Response of HttpConnection

I am wondering if the problem here is that you are not UTF-8 decoding this.

 

Your header says this:

Content-Type: text/plain; charset=utf-8

 

I would replace this line:

String temp = new String(buffer, 0, readIn);
with

String temp = "";

try {

temp = new String(buffer, 0, readIn, "UTF-8");

} catch (Exception e) {

// Will never get thrown, UTF-8 is a valid encoding

}

Let us know if this helps.

 

 

Developer
Posts: 33
Registered: ‎06-12-2009
My Device: Not Specified

Re: Parsing Response of HttpConnection

Sorry about the delay, another project took priority so it was a while before I could get back to this...

 

Peter, you are a genius. It was infact an encoding problem, and your solution fixed it.

 

Thank you very much.