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: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Can't upload image using Blackberry Internet.

I use the following code to connect to my server to upoad an image file to my server. It works well when I connect through Wifi. But when I disconnect wifi and turn on my mobile network (Blackberry Internet plan), it doesn't upload the image. Can anyone help me in this case? Thanks in advance.

 

Here is my code:

 

public static String getString()
	{
	 String connectionString = null;	   
	 String uid = null;
	// Wifi
	if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
	{
			connectionString = ";interface=wifi";
	}
	else
	{
	 ServiceBook sb = ServiceBook.getSB();
	 net.rim.device.api.servicebook.ServiceRecord[] records = sb.findRecordsByCid("WPTCP");
	 for (int i = 0; i < records.length; i++) {
		 if (records[i].isValid() && !records[i].isDisabled()) {
			 if (records[i].getUid() != null &&
			records[i].getUid().length() != 0) {
				 if ((records[i].getCid().toLowerCase().indexOf("wptcp") != -1) &&						 
						 (records[i].getUid().toLowerCase().indexOf("wifi") == -1) &&
						 (records[i].getUid().toLowerCase().indexOf("mms") == -1) ) {
				 			uid = records[i].getUid();
				 			break;
				 }
			 }
		 }
	 }
	 if (uid != null)
	 {
		 connectionString = ";deviceside=true;ConnectionUID="+uid;
	 }
	 else
	 {			 			 
		 // the carrier network
		 if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
		 {
			 String carrierUid = getCarrierBIBSUid();
				if (carrierUid == null) {
					// Has carrier coverage, but not BIBS. So use the
					// carrier's
					// TCP network					
					connectionString = ";deviceside=true";
				} else {
					// otherwise, use the Uid to construct a valid carrier
					// BIBS
					// request					
					connectionString = ";deviceside=false;connectionUID="
							+ carrierUid;
				}
		 } 

		 //(BlackBerry Enterprise Server)
		 else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
		 {
			 //connectionString = ";deviceside=false";
			 connectionString = ";deviceside=false;ConnectionTimeout=54321";
		 }	 
		// If there is no connection available abort to avoid bugging
		// the user unnecessarily.		 
		else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {			
		}
		// In theory, all bases are covered so this shouldn't be
		// reachable.
		else {
			//no other options found, assuming device			
			connectionString = ";deviceside=true";
		}
	 }
	}
	 return connectionString;
	 }


private static String getCarrierBIBSUid() {
		try {
			net.rim.device.api.servicebook.ServiceRecord[] records = ServiceBook.getSB().getRecords();
			int currentRecord;

			for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
				if (records[currentRecord].getCid().toLowerCase().equals(
						"ippp")) {
					if (records[currentRecord].getName().toLowerCase()
							.indexOf("bibs") >= 0) {
						return records[currentRecord].getUid();
					}
				}
			}
		} catch (Exception e) {
			System.out.println("Exception"+e.getMessage());
		}
		return null;
	}



 

And I use it like this:

 

StringBuffer connectionStr=new StringBuffer("http://www.myserver.com/upload_img.php");	
connectionStr.append(getString());

HttpConnection conn = (HttpConnection) Connector.open(connectionStr.toString(), Connector.READ_WRITE);

 

 

 

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

Re: Can't upload image using Blackberry Internet.

any special reason why you don't use connectionfactory?

are you aware of "it does not work" NOT being a good error description?
check the device log (alt+lglg) and your own output (any exceptions?)
----------------------------------------------------------
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: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Re: Can't upload image using Blackberry Internet.

Thanks simon for helping me again. What really happens is that, using the code I posted before, when the Wifi is turned off and mobile network is turned on, the application first uses my phone balance (ie. the money I have recharged it with), it does not use the internet plan I took from my service provider.  

 

And I was not aware of ConnectionFactory, will use it and then tell you the result.

Thanks again.

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Can't upload image using Blackberry Internet.

Hi Nikita,

 

Can you please detail of your webservice, i will do code then will put it here.

 

Thanks

Pawan

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

Re: Can't upload image using Blackberry Internet.

Regarding the internet plan, that would depend on what exactly it contains.
Some providers only allow traffic over a special APN etc.
That would be a configuration issue, most likely, the ConnectionFactory should make that easier as well.
----------------------------------------------------------
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: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Re: Can't upload image using Blackberry Internet.

[ Edited ]

I used ConnectionFactory now and it shows this exception when it tries to open InputStream:

 

Exception : Sending connection timed out after ~120000ms

 

My code to get connection:

 

final HttpConnection getConnection(String urlT) throws Exception {                
		connectionFactoryObjL = new ConnectionFactory();                        
		connectionFactoryObjL.setPreferredTransportTypes(new int[] {
				TransportInfo.TRANSPORT_MDS,
				TransportInfo.TRANSPORT_BIS_B,
				TransportInfo.TRANSPORT_TCP_WIFI,
				TransportInfo.TRANSPORT_TCP_CELLULAR,
				TransportInfo.TRANSPORT_WAP2, 
				TransportInfo.TRANSPORT_WAP
		}); 
		connectionFactoryObjL.setAttemptsLimit(2);
		connectionFactoryObjL.setTimeLimit(5000);
		connectionFactoryObjL.setTimeoutSupported(true);
		ConnectionDescriptor connectionDescriptor=connectionFactoryObjL.getConnection(urlT);
		HttpConnection connection=(HttpConnection) connectionDescriptor.getConnection();                
		return connection;                
	}

 

Code for sending image:

 

public synchronized void sendImg(String url){
		final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";
		HttpConnection conn=null;
		try{
			
			byte[] imagedata=getFileBytes(new StringBuffer("file:///store/home/user/_"+filename+".jpg"));
						
			//conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
			conn=ConnectToServer.getInstance().getConnection(url);
			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(imagedata.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=\"uploadedfile\";filename=\"_"+getCurrFilename()+".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(imagedata);
			out.write(newLine.getBytes());
			out.write("--".getBytes());
			out.write(BOUNDARY.getBytes());
			out.write("--".getBytes());
			out.write(newLine.getBytes());
			finalOut.write(out.toByteArray());
			
			System.out.println("Bytes Written");
			out.flush();
			out.close();

			finalOut.flush();
			finalOut.close();
			InputStream instream=conn.openInputStream();  //exception occurs here
			int ch=0;
			StringBuffer buffesr=new StringBuffer();			
			while((ch=instream.read())!=-1)
			{
				buffesr.append((char)ch);
			}	
			System.out.println("Server Response : "+new String(buffesr));				
		}
			catch (Exception e) {
			    // TODO: handle exception
				System.out.println("===Exception : "+e.getMessage());				
			}
		finally{
			try {
				conn.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("===IOException : "+e.getMessage());
			}
		}
		}

 

I call it like this:

 

StringBuffer connectionStr=new StringBuffer("http://www.myserver.com/upload_img.php?folder=");						
connectionStr.append(getFolderName());
			   			
sendImg(connectionStr.toString());

 

Please help..

 

 

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

Re: Can't upload image using Blackberry Internet.

remove the bis option.
----------------------------------------------------------
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: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Re: Can't upload image using Blackberry Internet.

[ Edited ]

Sorry for replying late. Removed the BIS option but still getting the same exception:

Exception : Local connection timed out after ~ 120000

 

What to do? 

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Can't upload image using Blackberry Internet.

Hi Nikita

 

Can you please give me your url. so that i may test at my end.

 

And, tell me also how to check if the image is properly uploaded on server.

 

Thanks

Pawan

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

Re: Can't upload image using Blackberry Internet.

In your code, before you try to open the input stream, can you please retrieve and get the Http return code.  Make sure this is OK (200), before you continue.

 

Why are you trying to retrieve input data?  Does your POST service really return some data when it has completed processing a POST?