views:

235

answers:

1

I used code given but I am getting "IOCancelledException" and "IOException". And IOCancelledException.getMessage() / IOException.getMessage() giving null string, it does not give error message. Please help me understaing reason.

class SMSThread extends Thread {
    Thread myThread;
    MessageConnection msgConn;
    String message;
    String mobilenumber;

    public SMSThread(String textMsg, String mobileNumber) {
        message = textMsg;
        mobilenumber = mobileNumber;
    }

    public void run() {
        try {
            msgConn = (MessageConnection) Connector.open("sms://+"
                    + mobilenumber);
            TextMessage text = (TextMessage) msgConn
                    .newMessage(MessageConnection.TEXT_MESSAGE);
            text.setPayloadText(message);
            msgConn.send(text);
            msgConn.close();
        } catch (IOCancelledException ioce) {
            System.out
                    .println("IOCancelledException: " + ioce.getMessage());
        } catch (IOException ioe) {
            System.out.println("IOException: " + ioe.getMessage());
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
+2  A: 

I've just tested sample, I cant reproduce exceptions.

Do you have running MDS simulator when you start device emulator?
(it's obligatory to simulate network connection)

Here is my code:

class Scr extends MainScreen {

    BasicEditField mPhoneNumberField = new BasicEditField("phone number", "");
    BasicEditField mSMSField = new BasicEditField("sms text", "");

    public Scr() {
        add(mPhoneNumberField);
        add(mSMSField);
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);

        menu.add(new MenuItem("send sms", 0, 0) {
            public void run() {
                SMSThread thread = new SMSThread(mSMSField.getText(),
                        mPhoneNumberField.getText());
                thread.start();
            }
        });
    }

    class SMSThread extends Thread {
        Thread myThread;
        MessageConnection msgConn;
        String message;
        String mobilenumber;

        public SMSThread(String textMsg, String mobileNumber) {
            message = textMsg;
            mobilenumber = mobileNumber;
        }

        public void run() {
            try {
                msgConn = (MessageConnection) Connector.open("sms://+"
                        + mobilenumber);
                TextMessage text = (TextMessage) msgConn
                        .newMessage(MessageConnection.TEXT_MESSAGE);
                text.setPayloadText(message);
                msgConn.send(text);
                msgConn.close();
            } catch (IOCancelledException ioce) {
                System.out
                        .println("IOCancelledException: " + ioce.getMessage());
            } catch (IOException ioe) {
                System.out.println("IOException: " + ioe.getMessage());
            } catch (Exception e) {
                System.out.println("Exception: " + e);
            }
        }
    }
}
Max Gontar
I have added below line to above code and it resolved my issue... msgConn = (MessageConnection)Connector.open("sms://:0"); text.setAddress("sms://+91"+mobilenumber);
vikram deshpande