Search…
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
1
//String uri = Requisition URL
2
//HttpMethod httpMethod = HTTP Verbs
3
PressListener getPressListener(final String url, String httpType) {
4
return (e) -> {
5
//msg variable will be responsible for storing the request response
6
String msg = "";
7
8
try {
9
10
HttpStream.Options options = new HttpStream.Options();
11
options.httpType = httpType;
12
13
HttpStream httpStream = new HttpStream(new URI(url), options);
14
ByteArrayStream bas = new ByteArrayStream(4096);
15
bas.readFully(httpStream, 10, 2048);
16
String data = new String(bas.getBuffer(), 0, bas.available());
17
18
Response<ResponseData> response = new Response<>();
19
response.responseCode = httpStream.responseCode;
20
21
if (httpStream.responseCode == 200){
22
response.data = (JSONFactory.parse(data, ResponseData.class));
23
24
//Accessing the answer and picking up the information.
25
msg += "Url: " + response.data.getUrl() + "\n";
26
msg += "Origin: " + response.data.getOrigin();
27
}
28
} catch (IOException e1) {
29
msg = "erro";
30
} catch (InstantiationException ex) {
31
ex.printStackTrace();
32
} catch (InvocationTargetException ex) {
33
ex.printStackTrace();
34
} catch (NoSuchMethodException ex) {
35
ex.printStackTrace();
36
} catch (IllegalAccessException ex) {
37
ex.printStackTrace();
38
}
39
40
lblResult.setText(msg);
41
lblResult.setRect(KEEP, KEEP, PREFERRED, PREFERRED);
42
43
};
44
}
45
46
public static class Response<T> {
47
public T data;
48
public int responseCode;
49
}
50
Copied!
Creating the packet to receive the response in order to handle the data obtained from the request
Structures
1
└── src
2
└── main
3
└── java
4
└── com.your_company_name.your_name_app
5
.
6
.
7
.
8
└── ResponseData
9
└── Args
10
└── ResponseData
Copied!
and now create the class to store the information
ResponseData class
Args class
1
package com.totalcross.RestApi.ResponseData;
2
3
public class ResponseData {
4
5
Args args;
6
String origin;
7
String url;
8
9
public Args getArgs() {
10
return args;
11
}
12
13
public void setArgs(Args args) {
14
this.args = args;
15
}
16
17
public String getOrigin() {
18
return origin;
19
}
20
21
public void setOrigin(String origin) {
22
this.origin = origin;
23
}
24
25
public String getUrl() {
26
return url;
27
}
28
29
public void setUrl(String url) {
30
this.url = url;
31
}
32
}
33
Copied!
1
package com.totalcross.RestApi.ResponseData;
2
3
public class Args {
4
5
private String args;
6
7
public String getArgs() {
8
return args;
9
}
10
11
public void setArgs(String args) {
12
this.args = args;
13
}
14
15
}
16
Copied!
Now just put that pressListener on your button by changing only the endpoint and the verb, see the example below
1
@Override
2
public void initUI() {
3
String binUrl = "http://httpbin.org";
4
5
Button btnGet = new Button("GET");
6
btnGet.addPressListener(getPressListener(binUrl + "/get", HttpStream.GET));
7
add(btnGet, LEFT, AFTER, FILL, fmH * 3);
8
9
Button btnPost = new Button("POST");
10
btnPost.addPressListener(getPressListener(binUrl + "/post", HttpStream.POST));
11
add(btnPost, LEFT, AFTER, FILL, fmH * 3);
12
13
Button btnPut = new Button("PUT");
14
btnPut.addPressListener(getPressListener(binUrl + "/put", HttpStream.PUT));
15
add(btnPut, LEFT, AFTER, FILL, fmH * 3);
16
17
Button btnDelete = new Button("DELETE");
18
btnDelete.addPressListener(getPressListener(binUrl + "/delete", HttpStream.DELETE));
19
add(btnDelete, LEFT, AFTER, FILL, fmH * 3);
20
21
22
lblResult = new Label(" ");
23
add(lblResult, LEFT, AFTER);
24
}
Copied!

References

  • See the complete API code remainder with TotalCross in GitHub.
Last modified 2yr ago