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
New Contributor
Posts: 3
Registered: ‎06-20-2013
My Device: 9300
My Carrier: Telenor
Accepted Solution

Uncaught exception on HttpConnection when URL is blocked

In my application, I am accessing a restful web service URL. Whenever the device is connected but the URL isn't accessible or non-responsive for any reason, I get an uncaught exception. All I want is to catch the exception and show a proper message. 

 

Ideally, I would like to

1. check if the URL is accessible or not

2. wait a bit longer if the URL is accessible but slow

 

Please suggest. Thanks.

 

Code: (relevant sections)

 

	public void fieldChanged(Field field, int context) {

		if (field == btnOK) {
			UiApplication.getUiApplication().invokeLater(new Runnable() {

				public void run() {
								String value=VerifyPinCode(rdPinCode.getText(),rd.getText());
										
								if (value.substring(0,4).equals("true")) {
                                                                        // do stuff
								} else {
									count = count++;
								}
                              }
			});

		} else if (field == btnCancel) {
			close();
		}
				
	}


	public String VerifyPinCode(String PinCode, String VRN) {
		try {

			if (WebService.CheckConn()) 
					{
						try{
					WebService wb = new WebService(
							"http://webservice.somewhere.com:8088/mywebservice.svc/WebTestMethod/"
								        + VRN
									+ "/" + PinCode);
     				       wb.start();
					wb.join();
					Responce = wb.Call();
					}
					catch(InterruptedException  ex)
					{
						Dialog.alert("No Internet Connection");						
					}
				}





public class WebService extends Thread {
	static HttpConnection con = null;
	static InputStream is = null;
	static StringBuffer rawResponse;
	String URL;
	public String resultant;

	WebService(String url) {
		URL = url + getConnectionString() + ";ConnectionTimeout=20000";

	}

	public void run() {

		try {
			con = (HttpConnection) Connector.open(URL, Connector.READ_WRITE,
					true);
			int rcc = con.getResponseCode();
						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			con = null;
			// Status.show(e.getMessage());
		}
		
		//catch (Exception ex) {
		// TODO Auto-generated catch block
		//con = null;
		// Status.show(e.getMessage());
		//}

	}

	public String Call() {
		try {

			// ConnectionTimeout=9000;";
			if (CheckConn()) {

				if (con == null) {
					rawResponse = new StringBuffer();
					rawResponse.append("Timed out");

				} else {
					is = con.openInputStream();
					// // Status.show("3");

					byte[] responseData = new byte[10000];
					int length = 0;
					rawResponse = new StringBuffer();
					while (-1 != (length = is.read(responseData))) {
						rawResponse.append(new String(responseData, 0, length));
						// Status.show("4");

					}
				}
			} else {
				Status.show("Internet service is not avaiable.");
			}
			// Status.show(result);

		} catch (Exception ex) {
			Status.show("Internet is not responding");
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (Exception e) {
				// Status.show(e.getMessage());
			}
		}
		return rawResponse.toString();

	}

	public String getConnectionString() {

		String connectionString = "";
		if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
			connectionString = ";interface=wifi";
		}

		else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
			connectionString = ";deviceside=false";
		} else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
			String carrierUid = getCarrierBIBSUid();
			if (carrierUid == null) {
				connectionString = ";deviceside=true";
			} else {
				connectionString = ";deviceside=false;connectionUID="
						+ carrierUid + ";ConnectionType=amdeeas-public";
			}
		} else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
			connectionString = ";None";

		}

		return connectionString;
	}

	public String getCarrierBIBSUid() {

		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();
				}
			}
		}

		return null;
	}

	public static boolean CheckConn() {
		try {
			if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
				return true;
			} else if (CoverageInfo.getCoverageStatus() >= 2) {
				return true;
			} else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_DIRECT) {
				return false; //false for device, true for simulator
			} else {
				return false;
			}
		} catch (Exception ex) {
			Dialog.alert("No Internet Connection");
			return false;
		}
	}

}

 

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

Re: Uncaught exception on HttpConnection when URL is blocked

remove the thread.join and the call to Call(), you block the UI and defeat the purpose of starting a thread at all.
To get a response from the thread you could use the callback pattern, for example.
----------------------------------------------------------
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
New Contributor
Posts: 3
Registered: ‎06-20-2013
My Device: 9300
My Carrier: Telenor

Re: Uncaught exception on HttpConnection when URL is blocked

Thank you. Can you please give some example of using callback from the thread? (Also if I remove the call to Call(), how else would that code be executed?)
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Uncaught exception on HttpConnection when URL is blocked

all code that the thread executes would be in the run function (or called from there).
you can check the httpdemo to see how a sample application uses this.
----------------------------------------------------------
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
New Contributor
Posts: 3
Registered: ‎06-20-2013
My Device: 9300
My Carrier: Telenor

Re: Uncaught exception on HttpConnection when URL is blocked

Ok got it. So the call to Call() will be in the run. So the httpdemo has the callback example? Where would I find this httpdemo?

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

Re: Uncaught exception on HttpConnection when URL is blocked

If you use eclipse it has an option for the sample apps.
you can also find them on the portal https://developer.blackberry.com/java/samples/
----------------------------------------------------------
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