Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Reading Text From server ( SpanLabel )
- // /src/com/mycompany/myapp/MyApplication.java
- //https://imgur.com/a/vANWoDj
- package com.mycompany.myapp;
- import com.codename1.components.SpanLabel;
- import com.codename1.io.ConnectionRequest;
- import static com.codename1.ui.CN.*;
- import com.codename1.ui.Display;
- import com.codename1.ui.Form;
- import com.codename1.ui.Dialog;
- import com.codename1.ui.Label;
- import com.codename1.ui.plaf.UIManager;
- import com.codename1.ui.util.Resources;
- import com.codename1.io.Log;
- import com.codename1.ui.Toolbar;
- import java.io.IOException;
- import com.codename1.ui.layouts.BoxLayout;
- import com.codename1.io.NetworkEvent;
- import com.codename1.io.NetworkManager;
- /**
- * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose
- * of building native mobile applications using Java.
- */
- public class MyApplication {
- private Form current;
- private Resources theme;
- public void init(Object context) {
- // use two network threads instead of one
- updateNetworkThreadCount(2);
- theme = UIManager.initFirstTheme("/theme");
- Toolbar.setGlobalToolbar(true);
- addNetworkErrorListener(err -> {
- // prevent the event from propagating
- err.consume();
- if(err.getError() != null) {
- Log.e(err.getError());
- }
- Log.sendLogAsync();
- Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
- });
- }
- public void start() {
- if(current != null){
- current.show();
- return;
- }
- Form TextForm = new Form("Hi World", BoxLayout.y());
- TextForm.add(new Label("Hi World"));
- String url="http://localhost/codenameoneRessource/text.txt";
- ConnectionRequest con=new ConnectionRequest();
- con.setUrl(url);
- con.addResponseListener((e)->{
- String response= new String(con.getResponseData());
- System.out.println(response); //print response in commandLine
- // using span label to auto return to Line / if text is short print "response" directly
- SpanLabel spanlb = new SpanLabel(); spanlb.setText(response);
- TextForm.add(spanlb);
- });
- NetworkManager.getInstance().addToQueueAndWait(con);
- TextForm.show();
- }
- public void stop() {
- current = getCurrentForm();
- if(current instanceof Dialog) {
- ((Dialog)current).dispose();
- current = getCurrentForm();
- }
- }
- public void destroy() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment