LogoLogo
v7.0.0
v7.0.0
  • TotalCross Overview
  • TotalCross Javadoc
  • TotalCross Changelog
  • Roadmap
  • Documentation
    • Getting Started
      • First embedded project with TotalCross
    • Components
      • Accordion
      • Aligned Labels
      • Button
      • Check
      • ComboBox
      • Dynamic Scroll
      • Edit
      • Floating Button
      • Gpiod
      • Grid
      • GridContainer
      • Image
      • ImageControl
      • ImageList
      • Label
      • Material Icons
      • Material Window
      • MessageBox
      • Multi Edit
      • Progress Bar
      • Progress Box
      • Radio
      • Radio Group
      • Scroll Container
      • Side Menu
      • Slider
      • Sliding Window
      • Spin List
      • Spinner
      • Switch
      • Tabbed Container
      • Velocimeter
    • APIs
      • API Overview
      • API Rest
      • Asynchronous Task
      • Camera
      • Control
        • Main Window
        • Window
        • Container
      • GPS
      • HTTPS and SSL
      • JSON
      • Maps
        • Maps - Deprecated
        • Static Map
      • Material Design Standards
      • Ninepath
      • Notifications
      • PrinterManager
      • Push Notification Firebase
      • Scanner
      • SOAP
      • Socket
      • SocketServer
      • SQLite Encryption
      • QR Code Generator
      • totalcross.sys
      • Youtube API
    • Creating an Issue
    • Contributing
      • Branch workflow
      • Writing documentation
    • Guides
      • App Architecture
        • Suggested Architecture
        • Why do Design Patterns help with the application's organization?
          • MVC Architecture Pattern
          • Template Pattern
          • Data Persistence: DAO Pattern.
        • Separation of concepts: What is the best way to create UI interfaces?
        • Positioning
          • Manual Positioning
        • Relative Positioning
        • Best practices to improve project maintenance
      • Device Simulator
      • Package your app from scratch
        • TotalCross SDK
        • Environment Variables in IDE
          • Eclipse
          • IntelliJ
        • Deploy your app with a dependecy TC
        • Deploy iOS
          • Using Development certificate to test your apps
      • Understanding TotalCross for Linux ARM
      • Running C++ applications with TotalCross
      • Web Services
    • Miscelaneous
      • Java JDK 8
      • Maven
      • Installing Visual Studio Code
    • FAQ
      • IMEI in Android 10
Powered by GitBook
On this page
  • Verbs
  • Requisitions
  • References

Was this helpful?

  1. Documentation
  2. APIs

API Rest

Verbs

HTTP verbs are the request methods we use along with the endpoints to access a particular api route.

Endpoint

Verbs

Action

/get

GET

Retrieves new information

/post

POST

Create new information

/put

PUT

Change an information

/delete

DELETE

Delete information

Requisitions

One of the ways to make requests is to create PressListener for Button, in the example below I will demonstrate how to get the response of the request in a variable and display in a Label

PressListener
//String uri = Requisition URL
//HttpMethod httpMethod = HTTP Verbs
    PressListener getPressListener(final String url, String httpType) {
        return (e) -> {
            //msg variable will be responsible for storing the request response
            String msg = "";

            try {

                HttpStream.Options options = new HttpStream.Options();
                options.httpType = httpType;

                HttpStream httpStream = new HttpStream(new URI(url), options);
                ByteArrayStream bas = new ByteArrayStream(4096);
                bas.readFully(httpStream, 10, 2048);
                String data = new String(bas.getBuffer(), 0, bas.available());

                Response<ResponseData> response = new Response<>();
                response.responseCode = httpStream.responseCode;
                
                if (httpStream.responseCode == 200){
                        response.data = (JSONFactory.parse(data, ResponseData.class));
                        
                        //Accessing the answer and picking up the information.
                        msg += "Url: " + response.data.getUrl() + "\n";
                        msg += "Origin: " + response.data.getOrigin();
                }
            } catch (IOException e1) {
                    msg = "erro";
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
            } catch (NoSuchMethodException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }

            lblResult.setText(msg);
            lblResult.setRect(KEEP, KEEP, PREFERRED, PREFERRED);

        };
    }
    
    public static class Response<T> {
        public T data;
        public int responseCode;
    }

Creating the packet to receive the response in order to handle the data obtained from the request

Structures
└── src
    └── main
        └── java
            └── com.your_company_name.your_name_app
                .
                .
                .
                └── ResponseData
                    └── Args
                    └── ResponseData

and now create the class to store the information

package com.totalcross.RestApi.ResponseData;

public class ResponseData {

    Args args;
    String origin;
    String url;

    public Args getArgs() {
        return args;
    }

    public void setArgs(Args args) {
        this.args = args;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

package com.totalcross.RestApi.ResponseData;

public class Args {

    private String args;

    public String getArgs() {
        return args;
    }

    public void setArgs(String args) {
        this.args = args;
    }

}

Now just put that pressListener on your button by changing only the endpoint and the verb, see the example below

@Override
    public void initUI() {
        String binUrl = "http://httpbin.org";

        Button btnGet = new Button("GET");
        btnGet.addPressListener(getPressListener(binUrl + "/get", HttpStream.GET));
        add(btnGet, LEFT, AFTER, FILL, fmH * 3);

        Button btnPost = new Button("POST");
        btnPost.addPressListener(getPressListener(binUrl + "/post", HttpStream.POST));
        add(btnPost, LEFT, AFTER, FILL, fmH * 3);

        Button btnPut = new Button("PUT");
        btnPut.addPressListener(getPressListener(binUrl + "/put", HttpStream.PUT));
        add(btnPut, LEFT, AFTER, FILL, fmH * 3);

        Button btnDelete = new Button("DELETE");
        btnDelete.addPressListener(getPressListener(binUrl + "/delete", HttpStream.DELETE));
        add(btnDelete, LEFT, AFTER, FILL, fmH * 3);


        lblResult = new Label(" ");
        add(lblResult, LEFT, AFTER);
    }

References

PreviousAPI OverviewNextAsynchronous Task

Last updated 5 years ago

Was this helpful?

See the complete API code remainder with TotalCross in .

GitHub