Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================StartUp.java===============================
- public class StartUp extends UiApplication
- {
- public static void main(String[]ali)
- {
- StartUp start=new StartUp();
- start.enterEventDispatcher();
- }
- public StartUp()
- {
- this.pushScreen(new LoadingScreen());
- }
- public static void exceptionHandling(final String exception)
- {
- UiApplication.getUiApplication().invokeLater(new Runnable()
- {
- public void run()
- {
- Dialog.alert(exception);
- }
- });
- }
- }
- //===================================LoadingScreen.java==============
- public class LoadingScreen extends MainScreen implements FieldChangeListener
- {
- ButtonField click;
- public LoadingScreen()
- {
- setTitle("Loading Screen");
- createGUI();
- }
- private void createGUI()
- {
- click=new ButtonField("click");
- click.setChangeListener(this);
- add(click);
- }
- public void fieldChanged(Field field, int context)
- {
- if(field==click)
- {
- ConnectionThread thread=new ConnectionThread(this,"http://startlogic.totemcat.net/sika/WebServices/sikainfo.php?category=0");
- thread.start();
- }
- }
- public boolean onMenu(int instance)
- {
- return true;
- }
- public boolean onClose()
- {
- return super.onClose();
- }
- }
- //==============================ConnectionThread.java=============
- public class ConnectionThread extends Thread
- {
- LoadingScreen loadingScreen;
- String url;
- HttpConnection httpConnection;
- InputStream inputStream;
- StringBuffer stringBuffer=new StringBuffer();
- public ConnectionThread(LoadingScreen loadingScreen, String url)
- {
- this.loadingScreen=loadingScreen;
- this.url=url;
- }
- public void run()
- {
- try
- {
- httpConnection=(HttpConnection) Connector.open(url+";interface=wifi");
- int response=httpConnection.getResponseCode();
- if(response==HttpConnection.HTTP_OK)
- {
- inputStream=httpConnection.openInputStream();
- int c;
- while((c=inputStream.read())!=-1)
- {
- stringBuffer.append((char)c);
- }
- callBack(stringBuffer.toString());
- }
- else
- {
- callBack("ERROR");
- }
- }
- catch (Exception e)
- {
- synchronized (UiApplication.getEventLock())
- {
- UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
- StartUp.exceptionHandling(e.getMessage());
- }
- }
- finally
- {
- try
- {
- if(httpConnection!=null)
- {
- httpConnection.close();
- }
- if(inputStream!=null)
- {
- inputStream.close();
- }
- }
- catch (Exception e2)
- {}
- }
- }
- private void callBack(String response)
- {
- if(response.equals("ERROR"))
- {
- UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
- StartUp.exceptionHandling("URL Not found");
- }
- else
- {
- try
- {
- System.out.println(response);
- //do what you want;
- }
- catch (Exception e)
- {
- StartUp.exceptionHandling("Data Not found");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement