nio Datagram read block in android api 10 - nio

This code works fine on donut (api 4) and Jelly beans ( api 17), but it stucks on GINGERBREAD (api 10), when it comes to sock.read(bb);
No exceptions are thrown. I can't figure out what's wrong with it.
(new Thread(new Runnable() { #Override public void run() {
try {
DatagramChannel sock= DatagramChannel.open();
sock.connect(new InetSocketAddress("127.0.0.1", 1910));
ByteBuffer bb = ByteBuffer.allocate(256);
sock.configureBlocking(false);
pr("SOCKET Read start!!!");
sock.read(bb);
pr("SOCKET Read passed!!!");
} catch (IOException e) {
pr("SOCKET BAD!!!");
e.printStackTrace();
}
}})).start();

Related

How to do Receipt printing using codenameone

I would like my codenameone based android application to print receipts using a rego bluetooth printer. Is there any plugin / extension that is able to do this or i might have to go the cnlib route
There is a bluetooth extension for codenameone since 2016. I am not sure if it has been updated, but you can check it out here https://www.codenameone.com/blog/bluetooth-support.html It gives a small test code to get started. Search for the CNIBluetooth extension, add it to your project then refresh libs.
final Bluetooth bt = new Bluetooth();
Form main = new Form("Bluetooth Demo");
main.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
main.add(new Button(new Command("enable bluetooth") {
#Override
public void actionPerformed(ActionEvent evt) {
try {
if (!bt.isEnabled()) {
bt.enable();
}
if (!bt.hasPermission()) {
bt.requestPermission();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}));
main.add(new Button(new Command("initialize") {
#Override
public void actionPerformed(ActionEvent evt) {
try {
bt.initialize(true, false, "bluetoothleplugin");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}));

Infinite progress hide complety the screen on android

on Android in a Form with a browserComponent the Infinite progress hide complety the screen and can't see the content, on IOS works fine.
If the form has no a BrowserComponent works fine in Android and IOS and the screen goes to dark but we can see the content.
I attach a sample code (only the start method)
public void start() {
if(current != null){
current.show();
return;
}
Form hi2 = new Form("No browser Form", BoxLayout.y());
hi2.add(new Button(new Command("Show Infinite Progress") {
#Override
public void actionPerformed(ActionEvent evt) {
try {
Dialog ip = new InfiniteProgress().showInfiniteBlocking();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ip.dispose();
}
}).start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}));
hi2.add(new Label("No browser Form"));
hi2.add(new Button(new Command("show browser Form") {
#Override
public void actionPerformed(ActionEvent evt) {
try {
Form hi = new Form("browser Form");
hi.setLayout(new BorderLayout());
BrowserComponent browserComponent = new BrowserComponent();
browserComponent.setURL("https://www.codenameone.com/");
hi.add(BorderLayout.CENTER, browserComponent);
hi.add(BorderLayout.SOUTH, new Button(new Command("Show Infinite Progress") {
#Override
public void actionPerformed(ActionEvent evt) {
try {
Dialog ip = new InfiniteProgress().showInfiniteBlocking();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ip.dispose();
}
}).start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}));
hi.show();
} catch (Exception ex) {
Dialog.show("Error", ex.getMessage(), "OK", null);
ex.printStackTrace();
}
}
}));
hi2.show();
}
InfiniteProgress creates a Dialog which shows it in the center but effectively blocks input all around. BrowserComponent doesn't work well with dialogs because the background of a dialog needs to be "painted" and isn't the real underlying Form.
Normally the workaround is to use an InteractionDialog. You can also use the LayeredPane to place the InfiniteProgress and even color it appropriately so it will "look" the same. Reproducing the blocking behavior is harder though. I'm not sure if you'll be able to do that since native widgets handle their own events. It's pretty easy to block input from Codename One components but by the time you get the event it might have been processed by the native widget.
The only workaround for that aspect I can think of is doing that part in JavaScript.

catching unknown host exception in codename one

I am building an app using codename one
So the thing is, I need to access a URL using the app. THe URL brings back some result which I show on the screen.
SO I use these lines to do that :
ConnectionRequest c = new ConnectionRequest() {
protected void readResponse(InputStream input) throws IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
int ch;
while ((ch = input.read()) != -1) {
bs.write(ch);
}
serverOutput = new String(bs.toByteArray());
bs.close();
}
};
c.setUrl("My URL HERE");
c.setPost(false);
NetworkManager.getInstance().addToQueueAndWait(c);
So, now , if the gprs is active, this code works fine.
BUT , if the GPRS is inactive, it throws an Unknow Host Exception
SO to catch this error, i TRIED to use a try catch block like this:
try{
NetworkManager.getInstance().addToQueueAndWait(c);
}
catch(Exception e)
{
Sys.out.pln(e.troString());
}
But, i still get the error in the form of a dialog in the app. How do i catch this error and put my own handling for it?
UPDATE 1:
Am not sure this is necessarily a codename one specific questions, or related to java ...so just help me out with this.
Try this to handle generic errors for all connections:
NetworkManager.getInstance().addErrorListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//handle your error here consume the event
evt.consume();
}
});
Or override:
protected void handleErrorResponseCode(int code, String message) {
}
And:
protected void handleException(Exception err) {
}
In your connection request code to do this for just one class.
Try it...
public void init(Object context) {
Display.getInstance().addEdtErrorHandler(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
evt.consume();
Throwable exception = (Throwable) evt.getSource();
}
});
}

Sending SMS from BlackBerry Simulator

I'm developing a BlackBerry Application where I should send Text SMS from BlackBerry Device.
As I'm new to Blackberry, started few days back I'm unable to proceed.
Can anyone Help with providing code snippets for send SMS from BlackBerry Device or Simulator?
Thanks in Advance.
Suresh.
public static void sendSMS(final String no, final String msg) {
// try {
new Thread() {
public void run() {
boolean smsSuccess = false;
if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
DatagramConnection dc = null;
try {
dc = (DatagramConnection) Connector.open("sms://" + no);
byte[] data = msg.getBytes();
Datagram dg = dc.newDatagram(dc.getMaximumLength());
dg.setData(data, 0, data.length);
dc.send(dg);
// / send successfully
smsSuccess = true;
} catch (Exception e) {
System.out.println("Exception 1 : " + e.toString());
e.printStackTrace();
smsSuccess = false;
} finally {
try {
dc.close();
dc = null;
} catch (IOException e) {
System.out.println("Exception 2 : " + e.toString());
e.printStackTrace();
}
}
} else {
MessageConnection conn = null;
try {
conn = (MessageConnection) Connector
.open("sms://" + no);
TextMessage tmsg = (TextMessage) conn
.newMessage(MessageConnection.TEXT_MESSAGE);
tmsg.setAddress("sms://" + no);
tmsg.setPayloadText(msg);
conn.send(tmsg);
smsSuccess = true;
} catch (Exception e) {
smsSuccess = false;
System.out.println("Exception 3 : " + e.toString());
e.printStackTrace();
} finally {
try {
conn.close();
conn = null;
} catch (IOException e) {
System.out.println("Exception 4 : " + e.toString());
e.printStackTrace();
}
}
}
if(smsSuccess)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Dialog.alert("success");
}
});
}else
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Dialog.alert("failure");
}
});
}
}
}.start();
}
Check out the the above code function .... to send SMS from Blackberry
You haven't specified what language you are developing in, but if you are developing in java and, if you are using Eclipse for your development with the Blackberry Java plugins, you will find a wealth of sample applications in the plugins folder hierarchy. The actual location will depend on where you have installed Eclipse, but e.g. on my machine they are at: C:\Program Files\Eclipse\eclipse 3.6.2 BlackBerry\plugins\net.rim.ejde.componentpack7.0.0_7.0.0.33\components\samples\com\rim\samples\device for the OS7 samples. Similar samples will exist for the different OS plugins you have installed.
There is a long standing sample in most OS sample sets called smsdemo which should give you all the code you need. Even if you are not developing in java, this sample should give you an indication of the path you need to take to fulfil your requirement.

Apache Camel - Exception - How caught an a exception

i'm newbie with apache camel (I'm using 2.8.1 version). I'm working with this framework and i understand (i hope) concept like route. Now i have this route definition
try {
context.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
// TODO Auto-generated method stub
from("vm:internal").
split().method(DefaultSplitLogic.class, "split").
dynamicRouter(bean(router, "route"));
}
});
}catch (DefaultSplitLogicException e) {
// TODO: handle exception
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This is the DefaultSpliLogic.class
public class DefaultSplitLogic {
public Object[] split(Object o) throws DefaultSplitLogicException{
if(o instanceof Collection<?>){
Collection c = (Collection) o;
return c.toArray();
}
else {
throw new DefaultSplitLogicException("Default Splitting Logic not correct");
}
}
}
This is DefaultSplitLogicException.class
public class DefaultSplitLogicException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
public DefaultSplitLogicException(String msg) {
// TODO Auto-generated constructor stub
super(msg);
System.err.println(msg);
}
public DefaultSplitLogicException(Throwable cause) {
super(cause);
}
}
I leaved router definition.
Now i want to capture my exception (i'm sure that my exception is thrown).
I'm using the onException clause into the route definition
try {
context.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
// TODO Auto-generated method stub
onException(DefaultSplitLogicException.class).handled(false);
from("vm:internal").
split().method(DefaultSplitLogic.class, "split").
dynamicRouter(bean(router, "route"));
}
});
}catch (DefaultSplitLogicException e) {
// TODO: handle exception
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but i cannot manage my exception. I tried to use differently this clause without success. Why?
Thank you all
I think i found the answer. It should be a bug of 2.8.1. version, fixed with 2.8.2+
http://camel.465427.n5.nabble.com/Cannot-handle-Exception-thrown-from-Splitter-Expression-td3286043.html

Resources