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
Highlighted
Contributor
Posts: 13
Registered: ‎02-08-2011
My Device: Bold 9780
My Carrier: Etisalat
Accepted Solution

BlockingSenderDestination.sendReceive() UTF-8 issue

I am loading JSON using the following method.

  

    private static Object loadJson(String uriStr){
        Object _json = null;
        Message response = null;
        BlockingSenderDestination bsd = null;

        try
        {
            bsd = (BlockingSenderDestination)
                        DestinationFactory.getSenderDestination
                            ("CommAPISample", URI.create(uriStr));

            if(bsd == null)
            {
                bsd =
                  DestinationFactory.createBlockingSenderDestination
                      (new Context("CommAPISample"),
                       URI.create(uriStr), new JSONMessageProcessor()
                       );
            }
            response = bsd.sendReceive();
            _json = response.getObjectPayload();       
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        finally
        {
            if(bsd != null)
            {
                bsd.release();
            }
        }
        return _json;
    }

This is working fine. But the problem is when I am getting JSON, Arabic characters show as junk

(الرئيس التنÙ) . I submitted this issue

http://supportforums.blackberry.com/t5/Java-Development/Arabic-shows-corrupted-in-the-JSON-output/m-...

As per the discussion, I encode the Arabic character into \uxxxx format and it was working. But now I have to access a JSON from somebody else where I can’t change the server side code.

They are using asp.net C# , as per them they are sending the data like the following.

 

            JsonResult result = new JsonResult();
            result.ContentEncoding = System.Text.Encoding.UTF8;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            result.Data = “Data Object (Contains Arabic) comes here”
            return result;

 

 

So my question is, If the server provide the data in the above manner,  BlockingSenderDestination.sendReceive method can get a utf-8 data? Or it is expecting only \uxxxx encoded data. Or I have to do something else (like sending some header to server) so that I can directly use the utf-8 data.

 

 In debug mode I check the value of 'response'. It is already showing junk characters.

Except from JSON I am able to handle Arabic everywhere else.

 

I am new to blackberry and Java. So I am sorry if this is silly question.

 

Thanks in advance.

Contributor
Posts: 13
Registered: ‎02-08-2011
My Device: Bold 9780
My Carrier: Etisalat

Re: BlockingSenderDestination.sendReceive() UTF-8 issue

Anybody here to help me.

Contributor
Posts: 13
Registered: ‎02-08-2011
My Device: Bold 9780
My Carrier: Etisalat

Re: BlockingSenderDestination.sendReceive() UTF-8 issue

At last I found the solution myself.

 

The data sending from server was in UTF-8 which uses double byte to show single character. But BlockingSenderDestination.sendReceive() is not able to identify that. So it is creating one character for each byte.

 

So the solution was to get each character and get the byte from that character and add to a byte array. From that byte array create a string with UTF8 encoding.

 

If anyone know to use BlockingSenderDestination.sendReceive() for utf-8 please post here. so that we can avoid this extra conversion method.

New Contributor
Posts: 3
Registered: ‎02-08-2011
My Device: Not Specified

Re: BlockingSenderDestination.sendReceive() UTF-8 issue

[ Edited ]

I am curious. What is the content type in the response? Is the server explicitly defining the UTF-8 character encoding? e.g.:

Content-Type: text/json; charset=UTF-8

If the API is ignoring the charset in the content type, an easier way to do the String conversion is by determining whether the Message received is a ByteMessage or a StreamMessage. Retrieve the message as a byte array and then convert to a string using the UTF-8 encoding

i.e.

Message msg = bsd.sendReceive();
byte[] msgBytes = null;

if (msg instanceof ByteMessage) {
    msgBytes = ((ByteMessage) msg).getBytePayload();
}
else { /* StreamMessage */
   // TODO read the bytes from the stream into a byte array
}
return new String(msgBytes,"UTF-8");

Contributor
Posts: 13
Registered: ‎02-08-2011
My Device: Bold 9780
My Carrier: Etisalat

Re: BlockingSenderDestination.sendReceive() UTF-8 issue

Thanks for the reply. Now I am also doing the same. But since I was new to JAVA and Blackberry development it took some time to do these. 

 

FYI

 

From the server I am explicitly defining the UTF-8 character encoding. The point I raised in the last post is I know exactly what is coming from server and I can do these extra coding to get UTF-8. But this is not the case always. Depending on the content type API has to do this. Well I don’t know this is normal in JAVA world. I am developing almost everything for Arabic (need UTF-8 always) sites and application. In dot net world I never had face these issues.