First of all, I have been trying to get a HTTP request in Flutter, and i have no experience in JavaScript.
A Website only provides a JavaScript sample code but i don't how to use it for Flutter.
https://kodestat.gitbook.io/flutter/flutter-http-requests-and-rest-api
I tried the above link but failed.
I know i have to insert URL and ServiceKey like this.
class HomePageState extends State<HomePage> {
//this async func will get data from the internet
//when our func is done we return a string
Future<String> getData() async {
//we have to wait to get the data so we use 'await'
http.Response response = await http.get(
//Uri.encodeFull removes all the dashes or extra characters present in our Uri
Uri.encodeFull("URL"),
headers: {
//if your api require key then pass your key here as well e.g "key": "my-long-key"
"Accept": "application/json"
"key": "my-long-key"
}
);
//print(response.body);
List data = json.decode(response.body);
//print(data);
print(data[1]["title"]);
}
But how should i handle encodeURIComponent and other functions in Flutter to get a HTTP request?
Here is JavaScript sample code.
var xhr = new XMLHttpRequest();
var url = 'http://openapi.tago.go.kr/openapi/service/ArvlInfoInqireService/getSttnAcctoArvlPrearngeInfoList'; /*URL*/
var queryParams = '?' + encodeURIComponent('ServiceKey') + '='+'ServiceKey'; /*Service Key*/
queryParams += '&' + encodeURIComponent('cityCode') + '=' + encodeURIComponent('25'); /*CityCode*/
queryParams += '&' + encodeURIComponent('nodeId') + '=' + encodeURIComponent('DJB8001793ND'); /*nodeID*/
xhr.open('GET', url + queryParams);
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
alert('Status: '+this.status+' Headers: '+JSON.stringify(this.getAllResponseHeaders())+' Body: '+this.responseText);
}
};
xhr.send('');