/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.CommandListener;
/**
* @author Miso
*/
public class cviko5_MIDLET extends MIDlet implements CommandListener {
private Display display;
private Command exit;
private Command start;
private Command zacni;
private Form form;
private Form mform;
private Alert alert;
TextField strPort;
String vkod = new String();
String login = new String("KAJ0020");
TextField logins;
String s,a;
TextField strLogin;
TextField strUrl;
public cviko5_MIDLET(){
}
public void initialize(){
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.EXIT, 1);
form = new Form("Customer Ranking");
//form.setCommandListener(this);
form.addCommand(exit);
form.addCommand(start);
//form.addCommand(vypis);
form.setCommandListener(this);
}
public void Http () throws IOException {
String kod =("KOD:");
try{
HttpConnection connection =(HttpConnection)Connector.open("http://homel.vsb.cz/~mor03/TAMZ.php?login=KAJ0020");
connection.setRequestMethod(HttpConnection.GET);
InputStream iStream = connection.openInputStream();
int rc = connection.getResponseCode();
if (rc == HttpConnection.HTTP_OK)
{
{System.out.println("navazani bylo uspesne");
}
int length = (int) connection.getLength();
System.out.println(length);
if (length >= 0)
{
byte [] data = new byte[length];
iStream.read(data);
s = new String(data);
System.out.println(s);
vkod = (kod +s);
form.append(vkod);
display.setCurrent(form);
}
}
}catch(Throwable t){
};
System.out.println(s);
System.out.println(login);
odpoved();
Forma2();
}
public void Forma2(){
try
{
alert = new Alert("",a, null, AlertType.INFO);
alert.setTimeout(5000);
}
catch(Exception e)
{
}
display.setCurrent(alert);
}
public void zacni(){
mform = new Form("login a server");
strLogin = new TextField("Login",null,64,TextField.ANY);
strLogin.setString("kaj0020");
strUrl = new TextField("Url",null,60,TextField.ANY);
strUrl.setString("http://homel.vsb.cz/~mor03/TAMZ.php");
mform.append(strLogin);
mform.append(strUrl);
zacni = new Command("OK", Command.OK,0);
mform.addCommand(zacni);
mform.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(mform);
}
public String odpoved(){
String res = "error comm";
StringBuffer sb = new StringBuffer();
String uri = "socket://lin-edu.cs.vsb.cz:2008";
try{
SocketConnection connect = (SocketConnection)Connector.open(uri,Connector.READ_WRITE);
OutputStream out = connect.openOutputStream();
out.write( ( "KAJ0020"+"\n"+s+"\n").getBytes() );
out.flush();
out.close();
InputStream in = connect.openInputStream();
int c = 0;
while ((c = in.read()) != -1) {
sb.append((char)c);
}
res = sb.toString();
connect.close();
in.close();
}
catch(Exception e){System.out.println(e.toString());}
System.out.println(res);
a=res;
return res;
}
public void startApp() {
zacni();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if (c==exit){
notifyDestroyed();}
if(c==start){
try{
Http();}
catch(Throwable t) {};
}
if(c==zacni){
try{
Http();}
catch(Throwable t) {};}
}
}