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
Posts: 13
Registered: ‎12-05-2009
My Device: bold 9000
My Carrier: Tigo
Accepted Solution

today, i have a problem with HTTP request

Well, this time i'm trying get a .png image from a URL. For get this i'm doing the following:

 

 

try{
if (CoverageInfo.isOutOfCoverage()){
setError("No hay covertura suficiente de la red");
errorFlag = true;
}
else{
c = (HttpConnection)Connector.open(url);
is = c.openDataInputStream();
for(int j=0;j<b.length-1;j++){
if (is.read()!=-1){
number = is.read();
integer = new Integer(number);
bite = integer.byteValue();
b[j] = bite;
}
else{
integer = new Integer(0);
bite = integer.byteValue();
b[j] = bite;
}
}
}
}catch(Exception e){
setError("no se obtubo el stream");
errorFlag = true;
}


try{
imagen = Bitmap.createBitmapFromBytes(b, 0, -1, 1);
}catch(Exception e){
setError("problema cargando imagen respondio:"+e);
errorFlag = true;
}

 

 

my problem is on the second "try" it returns:"java.lang.illegalArgumentException". I really dont know mucho about HTTP conections so i'm sure that i have a lot of wrong code

 

in other words i'm trying to pass to java language this short code on C++ language:

 

 

WebRequest request = System.Net.WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream datos = response.GetResponseStream();
Imagen = new Bitmap(datos);
pb1.Image = Imagen;
datos.Close();
response.Close();

 

can some one help me?

 

 

 

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: today, i have a problem with HTTP request

I suspect you are failing here:

 

createBitmapFromBytes(...)

 

Check the javadocs for that, what you have codes looks wrong to me.  And check that the byte array does contain the bytes you expect.  You may also like to review the sample in the httpconnection JavaDoc and compare with your code, or look at the one in httpdemo. 

 

You might find reviewing this Thread useful:

http://supportforums.blackberry.com/t5/Java-Development/Error-after-build-in-mobile/td-p/400120/page....

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: today, i have a problem with HTTP request

Here is a working sample:

http://www.coderholic.com/blackberry-webbitmapfield/

 

You might gain some insight from it.

 

 

New Developer
Posts: 13
Registered: ‎12-05-2009
My Device: bold 9000
My Carrier: Tigo

Re: today, i have a problem with HTTP request

thanks RexDoug and peter_strange,


in the book "J2ME java a tope" -unfortunately or fortunately (for me) it is in spanish-explain all about HTTP request and how interpret correctly the response. Finally with the following code, i could solved my problem:

 

 

try{
	//ask if i have coverage
	if (CoverageInfo.isOutOfCoverage()){
		setError("No hay covertura suficiente de la red");
		errorFlag = true;
	}
	else{
		//create connection
		c = (HttpConnection)Connector.open(url);
		c.setRequestMethod(HttpConnection.GET);
		is = c.openInputStream();
				
		//process response
		if(c.getResponseCode()==HttpConnection.HTTP_OK){
			size = (int)c.getLength();
			if(size != -1){
				data = new byte[size];
				is.read(data, 0, data.length);
				imagen = Bitmap.createBitmapFromBytes(data, 0, -1, 1);
			}
			else{
				ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
				int stream;
				while((stream=is.read())!=-1){
					byteArray.write(stream);
				}
				data = new byte[byteArray.size()];
				data = byteArray.toByteArray();
				byteArray.close();
				imagen = Bitmap.createBitmapFromBytes(data, 0, -1, 1);
			}
		}
		//close connections
		c.close();
		is.close();
	}
}

 

This code is similar to the example of rexdoug.