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: 157
Registered: ‎07-09-2009
My Device: Not Specified

Device hangs on while uploading image

[ Edited ]

Hey developers,

 

I am developing a applicaiton which allows users to upload their images from application to server.

 

I have developed and tested this in simulator and it works very fine. But, when I tried this is in device it taking too much time and I need to wait for long time or I need to remove battery in order to proceed.

 

Here is my code:

 

 

HttpConnection conn = null;
InputStream is = null;
Document doc;
try {
	
	FileConnection sendfconn = (FileConnection)Connector.open(Globals.previewImagePath);
   
	InputStream sendinput = sendfconn.openInputStream();
	byte[] datasend = new byte[(int)sendfconn.fileSize()];
	sendinput.read(datasend, 0, datasend.length);               
	sendfconn.close();                
	byte[] Bytes_Of_Image_To_Be_Uploaded = datasend;
   
	String boundary = "Some_Unique_Text_Also_Alphanumeric";
	String Url = "http://localhost/file_upload.php";
	conn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);conn.setRequestMethod(HttpConnection.POST);
	conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA+";boundary="+boundary);
	conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(Bytes_Of_Image_To_Be_Uploaded.length));
	conn.setRequestProperty( "x-rim-transcode-content", "none" );
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	OutputStream finalOut = conn.openOutputStream();
	
	String newLine = "\r\n";
	
	out.write(newLine.getBytes());
	
	out.write("--".getBytes());
	
	out.write(boundary.getBytes());
	
	out.write(newLine.getBytes());
	
	String contDisp="Content-Disposition:form-data; name=\"userfile\";filename=\1.jpg\"";
	
	String contEnc = "Content-Transfer-Encoding: binary";
	
	String type="Content-Type:image/jpeg";
	
	out.write(contDisp.getBytes());
	
	out.write(newLine.getBytes());
	
	out.write(type.getBytes());
	
	out.write(newLine.getBytes());
	
	out.write(contEnc.getBytes());
	out.write(newLine.getBytes());
	out.write(newLine.getBytes());
	out.write(Bytes_Of_Image_To_Be_Uploaded);
	out.write(newLine.getBytes());
	out.write("--".getBytes());
	out.write(boundary.getBytes());
	out.write("--".getBytes());
	out.write(newLine.getBytes());
	finalOut.write(out.toByteArray());
	out.flush();
	out.close();
	finalOut.flush();
	
	finalOut.close();
	int rc = conn.getResponseCode();
	if (rc != HttpConnection.HTTP_OK) {
		throw new ConnectionNotFoundException("Connection not available!");
	}
	DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	docBuilder.isValidating();
	doc = docBuilder.parse(conn.openInputStream());
	doc.getDocumentElement().normalize();
}
catch (Exception ioe) {
	try{
		throw new Exception(ioe);
	}catch(Exception e1){}
}
finally {
	if (is != null)
		try { is.close(); }
			catch (IOException ignored) {}
	if (conn != null)
		try { conn.close(); }
			catch (IOException ignored) {}
}

 

 

Is my code is correct? Or I need to change anything in my code to avoid this?

 

Am I doing correct or I need to send data as batch?

 

It is frustrating me...

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

Re: Device hangs on while uploading image

if you are not using a separate thread, do so. All blocking operations need not to be executed on the event thread.

----------------------------------------------------------
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: Device hangs on while uploading image

[ Edited ]

There is unfortunately more involved in getting the device to communicate that the simulator.  I recommend you review this Thread:

 

http://supportforums.blackberry.com/t5/Java-Development/Connecting-your-BlackBerry-http-and-socket-c...

 

and especially the required reading to understand the complexities better.

 

In this case, I would recommend that you try debugging on the device to see how far through your code you get. 

 

Looking at your code, I would suspect that you may have two problems:

a) You might be running on the event Thread

Remember that networking code must run on a separate Thread.  This typically doesn't cause a problem on the Simulator because the networking code runs quickly.

b) You need to set the connection suffix to match the connection method you are trying to use - see the Thread I pointed you at and the KB articles it references for more information.

 

Edit: Apologies for duplicating some of Simon's post. 

Developer
Posts: 157
Registered: ‎07-09-2009
My Device: Not Specified

Re: Device hangs on while uploading image

Other server call without thread is working fine.

 

Only file upload is not working as like normal server call.

 

Is it must to handle file upload in seperate thread?

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

Re: Device hangs on while uploading image

you should put all networking into separate threads.

----------------------------------------------------------
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: 157
Registered: ‎07-09-2009
My Device: Not Specified

Re: Device hangs on while uploading image

[ Edited ]

Thanks peter & simon.

 

I will do network related tasks in separate thread.

 

But I want to clear one thing, that how can I test my application as like actual Device. Because, it is very irritating me everytime I need to test in simulator and as well as Device. And more over, I don't have all the devices what my clients asking. I need to check in simulator only..

 

Please help me..

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

Re: Device hangs on while uploading image

You can't test fully wireless network code on the Simulator.  Period.  Nothing you can do about this.  You really do have to do this testing on device.

 

The only connection methods that are supported on the Simulator are direct TCP, BES/MDS and WiFi.  So you can't even simulate WAP or BIS-B on the SImulator.  But even a Direct TCP or BES connection that works on the Simulator is no guarantee that the connection will work on the device, even when the device supports that connection. 

 

I think most people write the network code once, then fine tune it as they implement it in applications.  The application should be as isolated as it can be from the network code. 

 

It is useful if you can supply the network code in a small application and try it out easily on various platforms as you get access to them.  This provides you with a way to test the network code easily;

 

It is also vital that your network code has good reporting, and debugging, so that you can find out what is happening, without needing to USB connect your customer's device. 

 

Review the networking sticky Thread again, there is lots of good advice in there.