iifast

MyApplication.java

Mar 8th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. // Reading Text From server ( SpanLabel )
  2. // /src/com/mycompany/myapp/MyApplication.java
  3. //https://imgur.com/a/vANWoDj
  4.  
  5. package com.mycompany.myapp;
  6.  
  7. import com.codename1.components.SpanLabel;
  8. import com.codename1.io.ConnectionRequest;
  9. import static com.codename1.ui.CN.*;
  10. import com.codename1.ui.Display;
  11. import com.codename1.ui.Form;
  12. import com.codename1.ui.Dialog;
  13. import com.codename1.ui.Label;
  14. import com.codename1.ui.plaf.UIManager;
  15. import com.codename1.ui.util.Resources;
  16. import com.codename1.io.Log;
  17. import com.codename1.ui.Toolbar;
  18. import java.io.IOException;
  19. import com.codename1.ui.layouts.BoxLayout;
  20. import com.codename1.io.NetworkEvent;
  21. import com.codename1.io.NetworkManager;
  22.  
  23. /**
  24.  * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose
  25.  * of building native mobile applications using Java.
  26.  */
  27. public class MyApplication {
  28.  
  29.     private Form current;
  30.     private Resources theme;
  31.  
  32.     public void init(Object context) {
  33.         // use two network threads instead of one
  34.         updateNetworkThreadCount(2);
  35.  
  36.         theme = UIManager.initFirstTheme("/theme");
  37.  
  38.         Toolbar.setGlobalToolbar(true);
  39.  
  40.         addNetworkErrorListener(err -> {
  41.             // prevent the event from propagating
  42.             err.consume();
  43.             if(err.getError() != null) {
  44.                 Log.e(err.getError());
  45.             }
  46.             Log.sendLogAsync();
  47.             Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
  48.         });        
  49.     }
  50.    
  51.     public void start() {
  52.         if(current != null){
  53.             current.show();
  54.             return;
  55.         }
  56.          Form TextForm = new Form("Hi World", BoxLayout.y());
  57.          TextForm.add(new Label("Hi World"));
  58.        
  59.         String url="http://localhost/codenameoneRessource/text.txt";
  60.         ConnectionRequest con=new ConnectionRequest();
  61.         con.setUrl(url);
  62.         con.addResponseListener((e)->{
  63.         String response= new String(con.getResponseData());
  64.         System.out.println(response); //print response in commandLine
  65.         // using span label to auto return to Line / if text is short print "response" directly
  66.         SpanLabel spanlb = new SpanLabel(); spanlb.setText(response);
  67.         TextForm.add(spanlb);
  68.  
  69.         });
  70.         NetworkManager.getInstance().addToQueueAndWait(con);
  71.        
  72.    
  73.         TextForm.show();
  74.     }
  75.  
  76.     public void stop() {
  77.         current = getCurrentForm();
  78.         if(current instanceof Dialog) {
  79.             ((Dialog)current).dispose();
  80.             current = getCurrentForm();
  81.         }
  82.     }
  83.    
  84.     public void destroy() {
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment