Уважаемый форумчане, уж помогите начинающему, как же все таки верно отправить данные методом POST( или GET главное отправить на www.xxx.ru/test.php) Перечитал форум нашел куски кодов, но все равно нужно же как то прописывать. Например HttpConnection con = null; и тому подобное... Как сделать соединение?
Суть вот в чем есть програмка в ней нужно организовать регистрацию пользователя. Отправляем Номер телефона Номер машины и Имя
Код | import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.DataOutputStream; import javax.microedition.lcdui.ItemStateListener; import javax.microedition.lcdui.Item.*; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; import javax.microedition.io.*; import javax.microedition.io.ConnectionNotFoundException; import java.io.InputStream; import java.io.ByteArrayOutputStream;
public class MentOFF extends MIDlet implements CommandListener {
private static final Command CMD_EXIT = new Command("Выход", Command.EXIT, 1); private static final Command CMD_BACK = new Command("Назад", Command.BACK, 1); private static final Command CMD_OK = new Command("ОК", Command.OK, 3); private static final Command CMD_OK2 = new Command("Зарегистрировать", Command.OK, 4); private static final Command CMD_OK1 = new Command("Сдать", Command.OK, 5); private Display display; private List mainList; private List exclusiveList; private Form implicitList; private Form multipleList; private Form registration; private Form enter; private boolean firstTime; private String InputStream; HttpConnection con = null; HttpConnection os;
private TextField data; private TextField phone; private TextField myname; private TextField carn;
public MentOFF() { display = Display.getDisplay(this);
String[] stringArray = { "Порт" }; Image[] imageArray = null;
exclusiveList = new List("", Choice.EXCLUSIVE, stringArray, imageArray); exclusiveList.addCommand(CMD_BACK); exclusiveList.addCommand(CMD_EXIT); exclusiveList.addCommand(CMD_OK1); exclusiveList.setCommandListener(this);
implicitList = new Form("Заголовок"); implicitList.append("АПР"); implicitList.addCommand(CMD_BACK); implicitList.setCommandListener(this);
multipleList = new Form("Ограничение"); multipleList.append("???"); multipleList.addCommand(CMD_BACK); multipleList.setCommandListener(this);
registration = new Form("Регистрация"); registration.append("Введите имя:"); registration.append(myname = new TextField("", "", 15, TextField.ANY)); registration.append("Номер телефона:"); registration.append(phone = new TextField("", "+7", 12, TextField.PHONENUMBER)); registration.append("Цифры номера машины:"); registration.append(carn = new TextField("", "", 3, TextField.PHONENUMBER)); registration.addCommand(CMD_OK2); registration.addCommand(CMD_BACK); registration.addCommand(CMD_EXIT); registration.setCommandListener(this); firstTime = true;
enter = new Form("Вход"); enter.append("Введите Имя:"); enter.addCommand(CMD_BACK); enter.addCommand(CMD_EXIT); enter.setCommandListener(this); firstTime = true; }
private void makeHttpPost(){ String TelNum = phone.getString(); String CarNum = carn.getString(); String MyNameg = myname.getString(); String text = ("Начало регистрации. Пожалуйста подождите"); Alert a = new Alert("URL", text, null, AlertType.INFO); display.setCurrent(a); con=(HttpConnection) Connector.open("http://domen.com/index.php"); con.setRequestMethod(HttpConnection.GET); con.setRequestProperty("Accept","*/*"); con.setRequestProperty("Connection", "close"); int code=con.getResponseCode(); is=con.openInputStream(); }
protected void startApp() { if (firstTime) { Image[] imageArray = null;
try { Image icon = Image.createImage("/Icon.png");
imageArray = new Image[] { icon, icon, icon, icon, icon }; } catch (java.io.IOException err) {
}
String[] stringArray = { "Меню", "Меню", "Меню", "Регистрация", "Вход" }; mainList = new List("Choose type", Choice.IMPLICIT, stringArray, imageArray); mainList.addCommand(CMD_EXIT); mainList.setCommandListener(this); display.setCurrent(mainList); firstTime = false; } }
protected void destroyApp(boolean unconditional) { }
protected void pauseApp() { }
public void commandAction(Command c, Displayable d) { if (d.equals(mainList)) { // in the main list if (c == List.SELECT_COMMAND) { if (d.equals(mainList)) { switch (((List)d).getSelectedIndex()) { case 0: display.setCurrent(exclusiveList);
break;
case 1: display.setCurrent(implicitList);
break;
case 2: display.setCurrent(multipleList);
break; case 3: display.setCurrent(registration);
break; case 4: display.setCurrent(enter);
break; } } } } else { // in one of the sub-lists if (c == CMD_BACK) { display.setCurrent(mainList); } } if (c == CMD_EXIT) { destroyApp(false); notifyDestroyed(); } if (c == CMD_OK2) { makeHttpPost(); }
} }
|
Собственно все пока работает, кроме отправки данных. Уж если кто сталкивался, объясните новичку. |