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
New Developer
david_schwartz_bb
Posts: 137
Registered: ‎02-04-2010
My Device: Tour 9630
My Carrier: verizon
Accepted Solution

convert StreamConnection to String

How can I convert StreamConnection xml that is returned from an http call to a String?

Currently I call:

fconn = (StreamConnection)Connector.open(xmlUrl);

which returns xml. I parse the xml & it works fine.

I also want to save the xml string.

Thanks

Please use plain text.
Developer
Kiran_Snist
Posts: 174
Registered: ‎01-25-2011
My Device: Not Specified

Re: convert StreamConnection to String

Try using this..

 

 

HttpConnection fconn=(HttpConnection)Connector.open(Xmlurl)



int status = fconn.getResponseCode();


if (status == HttpConnection.HTTP_OK) {
   InputStream httpInput=fconn.openInputStream();
						
     StringBuffer buffer = new StringBuffer();

int ch = 0;
  while (ch != -1) {
 ch = httpInput.read();
					                  buffer.append((char) ch);
}

String response=buffer.toString();
}


 I am not sure.. but i have used that code to extract JSON String from server.. it might also work for XML String!!

 

Please use plain text.
Developer
dx22
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: convert StreamConnection to String

[ Edited ]

It will be easiest to use:

 

 

InputStream is = fconn.openInputStream();
byte[] streamBytes = IOUtilities.streamTobytes(is);
String str = new String(streamBytes);

This is only for the stream to string conversion part of the code.

 

----------------------------------------------------------------------
Press the button to give kudos if I helped you :smileyhappy:
Please use plain text.
Contributor
hrnt
Posts: 23
Registered: ‎02-22-2011
My Device: Not Specified

Re: convert StreamConnection to String

 


Kiran_Snist wrote:

Try using this..

 

 

HttpConnection fconn=(HttpConnection)Connector.open(Xmlurl)



int status = fconn.getResponseCode();


if (status == HttpConnection.HTTP_OK) {
   InputStream httpInput=fconn.openInputStream();
						
     StringBuffer buffer = new StringBuffer();

int ch = 0;
  while (ch != -1) {
 ch = httpInput.read();
					                  buffer.append((char) ch);
}

String response=buffer.toString();
}


 I am not sure.. but i have used that code to extract JSON String from server.. it might also work for XML String!!

 


 

By the way, there is a bug in your code (in the while loop). The problem is that your order of operations is this

 

while (input operation succeeded) {

  read next character

  store character to buffer

}

 

The problem is that when your read operation returns -1, you store the -1 to the buffer and only after that you check if the operation succeeded. Now your string buffer always has the (char) -1 as the last character.

 

 (In any case the IOUtilities-based solution is better)

Please use plain text.