Sunday, February 20, 2011

way2sms complete code

ackage hello;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.InputConnection;
import javax.microedition.io.OutputConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class HelloMIDlet extends MIDlet
implements CommandListener
{
private boolean midletPaused;
private Command exitCommand;
private Command screenCommand;
private Command screenCommand1;
private Command exitCommand1;
private Command backCommand;
private Command okCommand;
private Command okCommand1;
private Command addCommand;
private Command doneCommand;
private Command okCommand4;
private Command okCommand2;
private Command okCommand3;
private Command okCommand5;
private Command exitCommand2;
private Form form;
private TextField textField1;
private TextField textField;
private StringItem stringItem;
private Alert alert;
private Form form1;
private TextField textField2;
private TextField textField3;
private StringItem stringItem1;
private Spacer spacer;
private ImageItem imageItem;
private Form multisms;
private TextField cnum;
private TextField nums;
private Alert alert1;
private Form form2;
private TextField textField4;
private TextField object;
private Font font1;
private Image image2;
private Image image1;

public HelloMIDlet()
{
this.midletPaused = false;
}

private void initialize()
{
}

public void startMIDlet()
{
switchDisplayable(null, getForm1());
}

public void resumeMIDlet()
{
}

public void switchDisplayable(Alert alert, Displayable nextDisplayable)
{
Display display = getDisplay();
if (alert == null)
{
display.setCurrent(nextDisplayable);
}
else
display.setCurrent(alert, nextDisplayable);
}

public void commandAction(Command command, Displayable displayable)
{
if (displayable == this.alert)
{
if (command == this.backCommand)
{
this.textField1.setString("");
switchDisplayable(null, getForm());
}
else if (command == this.okCommand3)
{
switchDisplayable(null, getForm());
}
}
else if (displayable == this.form)
{
if (command == this.exitCommand)
{
switchDisplayable(null, getForm1());
this.textField1.setString(null);
}
else if (command == this.okCommand)
{
switchDisplayable(null, getMultisms());
this.nums.setString(this.textField.getString());
}
else if (command == this.okCommand1)
{
this.textField1.setString("");
}
else if (command == this.okCommand2)
{
this.textField.setString("");
}
else if (command == this.screenCommand)
{
switchDisplayable(getAlert(), getForm());
String rlt = null;
try
{
String mob = this.textField.getString();
mob = mob.replace('#', ';');
if ((mob.length() > 10) && (mob.length() < 15)) { mob = mob.substring(mob.length() - 10); } this.stringItem.setText("Sending SMS to " + mob); rlt = sendSMS(this.textField2.getString(), this.textField3.getString(), mob, this.textField1.getString()); } catch (IOException ex) { ex.printStackTrace(); } this.stringItem.setText(rlt); this.alert.setString(rlt); } } else if (displayable == this.form1) { if (command == this.exitCommand1) { exitMIDlet(); } else if (command == this.screenCommand1) { switchDisplayable(null, getForm()); } } else if (displayable == this.form2 ? (command != this.exitCommand2) && (command != this.okCommand5) : displayable == this.multisms) { if (command == this.addCommand) { String cur = this.nums.getString(); String num = this.cnum.getString(); if (num.length() < 10) { switchDisplayable(getAlert1(), getMultisms()); } else { num = num.substring(num.length() - 10); if (num.length() == 10) { if (cur.equals("")) { cur = cur + num; } else { cur = cur + "#" + num; } this.nums.setString(cur); this.cnum.setString(""); } else { switchDisplayable(getAlert1(), getMultisms()); } } } else if (command == this.doneCommand) { this.textField.setString(this.nums.getString()); this.nums.setString(""); switchDisplayable(null, getForm()); } else if (command == this.okCommand4) { this.nums.setString(""); } } } public Command getExitCommand() { if (this.exitCommand == null) { this.exitCommand = new Command("Logout", 4, 4); } return this.exitCommand; } public Form getForm() { if (this.form == null) { this.form = new Form("SMS appln by shajis001", new Item[] { getTextField(), getTextField1(), getStringItem() }); this.form.addCommand(getExitCommand()); this.form.addCommand(getScreenCommand()); this.form.addCommand(getOkCommand()); this.form.addCommand(getOkCommand1()); this.form.addCommand(getOkCommand2()); this.form.setCommandListener(this); } return this.form; } public Command getScreenCommand() { if (this.screenCommand == null) { this.screenCommand = new Command("Send", 2, 0); } return this.screenCommand; } public TextField getTextField() { if (this.textField == null) { this.textField = new TextField("Mobile No", null, 600, 3); } return this.textField; } public TextField getTextField1() { if (this.textField1 == null) { this.textField1 = new TextField("Msg", null, 140, 0); this.textField1.setLayout(0); this.textField1.setPreferredSize(-1, 120); } return this.textField1; } public Alert getAlert() { if (this.alert == null) { this.alert = new Alert("alert", null, getImage1(), null); this.alert.addCommand(getBackCommand()); this.alert.addCommand(getOkCommand3()); this.alert.setCommandListener(this); this.alert.setTimeout(-2); } return this.alert; } public StringItem getStringItem() { if (this.stringItem == null) { this.stringItem = new StringItem("", "140 Characters Remainig"); this.stringItem.setFont(getFont1()); } return this.stringItem; } public Command getExitCommand1() { if (this.exitCommand1 == null) { this.exitCommand1 = new Command("Exit", 7, 0); } return this.exitCommand1; } public Command getScreenCommand1() { if (this.screenCommand1 == null) { this.screenCommand1 = new Command("Login", 1, 0); } return this.screenCommand1; } public Form getForm1() { if (this.form1 == null) { this.form1 = new Form("Login", new Item[] { getImageItem(), getTextField2(), getTextField3(), getSpacer(), getStringItem1() }); this.form1.addCommand(getExitCommand1()); this.form1.addCommand(getScreenCommand1()); this.form1.setCommandListener(this); } return this.form1; } public TextField getTextField2() { if (this.textField2 == null) { this.textField2 = new TextField("Mobile", null, 32, 2); this.textField2.setInitialInputMode("UCB_BASIC_LATIN"); } return this.textField2; } public TextField getTextField3() { if (this.textField3 == null) { this.textField3 = new TextField("Password", null, 32, 65536); } return this.textField3; } public Font getFont1() { if (this.font1 == null) { this.font1 = Font.getFont(0, 0, 8); } return this.font1; } public StringItem getStringItem1() { if (this.stringItem1 == null) { this.stringItem1 = new StringItem("Done by", "shajis001", 1); this.stringItem1.setFont(getFont1()); } return this.stringItem1; } public Spacer getSpacer() { if (this.spacer == null) { this.spacer = new Spacer(16, 1); this.spacer.setPreferredSize(-1, 30); } return this.spacer; } public Command getBackCommand() { if (this.backCommand == null) { this.backCommand = new Command("Clear Msg", 2, 0); } return this.backCommand; } public Form getMultisms() { if (this.multisms == null) { this.multisms = new Form("Select Multiple Sender", new Item[] { getCnum(), getNums() }); this.multisms.addCommand(getDoneCommand()); this.multisms.addCommand(getAddCommand()); this.multisms.addCommand(getOkCommand4()); this.multisms.setCommandListener(this); } return this.multisms; } public TextField getNums() { if (this.nums == null) { this.nums = new TextField("Multiple Senders(# to seperate)", null, 600, 3); this.nums.setPreferredSize(-1, 220); } return this.nums; } public Command getOkCommand() { if (this.okCommand == null) { this.okCommand = new Command("MultiSender", 4, 1); } return this.okCommand; } public Command getOkCommand1() { if (this.okCommand1 == null) { this.okCommand1 = new Command("Clear Msg", 4, 2); } return this.okCommand1; } public TextField getCnum() { if (this.cnum == null) { this.cnum = new TextField("Select From Contact", null, 32, 3); } return this.cnum; } public Command getDoneCommand() { if (this.doneCommand == null) { this.doneCommand = new Command("Done", 4, 0); } return this.doneCommand; } public Command getAddCommand() { if (this.addCommand == null) { this.addCommand = new Command("Add Number", 2, 0); } return this.addCommand; } public Alert getAlert1() { if (this.alert1 == null) { this.alert1 = new Alert("!!!Warning!!!", "Plz Provide a 10 Digit Number", null, AlertType.WARNING); this.alert1.setTimeout(-2); } return this.alert1; } public Command getOkCommand2() { if (this.okCommand2 == null) { this.okCommand2 = new Command("Clear Numbers", 4, 3); } return this.okCommand2; } public Command getOkCommand3() { if (this.okCommand3 == null) { this.okCommand3 = new Command("Done", 4, 0); } return this.okCommand3; } public Command getOkCommand4() { if (this.okCommand4 == null) { this.okCommand4 = new Command("Clear Numbers", 4, 0); } return this.okCommand4; } public Form getForm2() { if (this.form2 == null) { this.form2 = new Form("form2", new Item[] { getTextField4(), getObject() }); this.form2.addCommand(getOkCommand5()); this.form2.addCommand(getExitCommand2()); this.form2.setCommandListener(this); } return this.form2; } public TextField getTextField4() { if (this.textField4 == null) { this.textField4 = new TextField("Provide 10 Digit Mobile Number", null, 32, 0); this.textField4.setPreferredSize(-1, 80); } return this.textField4; } public TextField getObject() { if (this.object == null) { this.object = new TextField("Look Up Details", null, 32, 0); this.object.setPreferredSize(-1, 140); } return this.object; } public Command getOkCommand5() { if (this.okCommand5 == null) { this.okCommand5 = new Command("Get", 4, 0); } return this.okCommand5; } public Command getExitCommand2() { if (this.exitCommand2 == null) { this.exitCommand2 = new Command("Exit", 7, 0); } return this.exitCommand2; } public Image getImage1() { if (this.image1 == null) { try { this.image1 = Image.createImage("/sms-big.gif"); } catch (IOException e) { e.printStackTrace(); } } return this.image1; } public ImageItem getImageItem() { if (this.imageItem == null) { this.imageItem = new ImageItem("", getImage2(), 0, "", 0);
}
return this.imageItem;
}

public Image getImage2()
{
if (this.image2 == null)
{
try
{
this.image2 = Image.createImage("/way2sms.gif");
}
catch (IOException e)
{
e.printStackTrace();
}
}
return this.image2;
}

public Display getDisplay()
{
return Display.getDisplay(this);
}

public void exitMIDlet()
{
switchDisplayable(null, null);
destroyApp(true);
notifyDestroyed();
}

public void startApp()
{
if (this.midletPaused)
{
resumeMIDlet();
}
else {
initialize();
startMIDlet();
}
this.midletPaused = false;
}

public void pauseApp()
{
this.midletPaused = true;
}

public void destroyApp(boolean flag)
{
}

public static String sendSMS(String uid, String pwd, String mob, String msg)
throws IOException
{
HttpConnection httpConn = null;
String url = "http://122.170.122.191.www.concealme.com/nph-proxy.pl/0A/http/demo.samplephpcodes.com/sms/way2sms.php?uid=9600871969" + uid + "&pwd=" + pwd;
InputStream is = null;
OutputStream os = null;
String out = "";
url = url + "&phone=" + mob + "&msg=" + urlEncode(msg);
httpConn = (HttpConnection)Connector.open(url);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Confirguration/CLDC-1.1");
getConnectionInformation(httpConn);
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
StringBuffer sb = new StringBuffer();
os = httpConn.openOutputStream();
is = httpConn.openDataInputStream();
int chr;
while ((chr = is.read()) != -1)
{
sb.append((char)chr);
}
System.out.println("Status" + sb.toString());
out = sb.toString();
}
else {
System.out.println("Error in opening HTTP Connection. Error#" + respCode);
}
if (is != null)
{
is.close();
}
if (os != null)
{
os.close();
}
if (httpConn != null)
{
httpConn.close();
}

if (is != null)
{
is.close();
}
if (os != null)
{
os.close();
}
if (httpConn != null)
{
httpConn.close();
}
return out;
}

public static String urlEncode(String s)
{
if (s == null)
{
return s;
}

StringBuffer sb = new StringBuffer(100);
for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) { sb.append(c); } else if (c > '\017')
{
sb.append("%" + Integer.toHexString(c));
}
else {
sb.append("%0" + Integer.toHexString(c));
}
}

return sb.toString();
}

public static void getConnectionInformation(HttpConnection httpconnection)
{
}
}

No comments:

Post a Comment