FLUTTER API: can't use ".body" - json

I can't use ".body" , even if im trying to just "print(result.body);"
Im getting this error message:
String url = "https://jsonplaceholder.typicode.com/posts/1/comments";
var result = http.get(Uri.parse(url));
apiList = jsonDecode(result.body)
I have no idea how to fix, any help is welcomed.
If you need aditional information, im ready to provide

make your function async, then
var result = await http.get(Uri.parse(url));

Related

Xamarin get webservice with parameter

i am new with xamarin. I want to add some json data with get web service. I try like following:
var response = await client.GetAsync("myurl" + "?applicationid=" +
applicationId + "?siteid=" + siteId + "?userid=" + userId");
string responseJson = await response.Content.ReadAsStringAsync();
Debug.WriteLine("response:>" + responseJson);
But code not executed after get method.
please anyone help me.
Thanks in advance. :)
Finally got the solution, pass the values with the url itself.
var response = await client.GetAsync("mybaseurl"+"/applicationid/"1"/siteid/"5"/u‌​serid/"+25");

how to use HttpClient in WindowsPhone 8

Hey everyone i have a big problem with this thing. i'm trying to use HttpClient method in my new project. i try this code:
var httpClient = new HttpClient();
var request = await httpClient.GetAsync(new Uri("http://www.google.com/", UriKind.RelativeOrAbsolute));
var txt = request.Content.ReadAsStringAsync();
MessageBox.Show(txt.Result);
i think it is true code because i wrote it in Console app and it works fine. Then i opened a new WindowsPhone 8 project and write this code.And code doesn't work, it returns Null. Sometimes it works but generally not. i thought my Visual Studio wasnt work good and i deleted it and re-install it, and nothing had been changed. what do you think?
try this.
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(new Uri("http://www.google.com/", UriKind.RelativeOrAbsolute));
response.EnsureSuccessStatusCode();
var txt = response.Content.ReadAsStringAsync();
MessageBox.Show(txt.Result);
make the breakpoint in the line response.EnsureSuccessStatusCode(); to make sure response httpcode is 200 every time.
You shouldn't be calling Result. Try this instead:
var httpClient = new HttpClient();
var request = await httpClient.GetAsync(new Uri("http://www.google.com/", UriKind.RelativeOrAbsolute));
var txt = await request.Content.ReadAsStringAsync();
MessageBox.Show(txt);

IronJS: How to parse JSON(send as string parameter) to a method in JavaScript function

I am new to IronJS and facing difficulty parsing JSON in JavaScript method.
My C# Code
string jsonString = "{\"Name\": \"Ankur\", \"Sex\": \"Male\"}";
var o = new IronJS.Hosting.CSharp.Context();
o.ExecuteFile(#"C:\CustomScript.js");
var handleJson = o.Globals.GetT<FunctionObject>("HandleJson");
var result = handleJson.Call(o.Globals, jsonString).Unbox<string>();
Console.WriteLine(result);
JavaScript method in CustomScript.js
function HandleJson(jsonStr) {
obj = JSON.parse(jsonStr);
return obj.Name;
}
Everytime I do this, I get error message saying "ReferenceError: JSON is not defined"
Guess, "JSON.parse" method is native to browser and which isn't available server side.
I can use jQuery method obj = $.parseJSON(jsonStr); as well but don't know how to load jQuery file.
Any thoughts on what I am doing wrong or how to fix it?
Thanks.
I have found the fix to it.
JSON.parse is an unknown JS method at Server(which is why we were getting the error)... So, we need to add/load "json2.js" before CustomScript.js file and then we will be good.
json2.js can be downloaded from following location: https://github.com/douglascrockford/JSON-js
Following is my updated code.
Updated C# Code
string jsonString = "{\"Name\": \"Ankur\", \"Sex\": \"Male\"}";
var o = new IronJS.Hosting.CSharp.Context();
o.ExecuteFile(#"C:\json2.js");
o.ExecuteFile(#"C:\CustomScript.js");
var handleJson = o.Globals.GetT<FunctionObject>("HandleJson");
var result = handleJson.Call(o.Globals, jsonString).Unbox<string>();
Console.WriteLine(result);
No changes are required in CustomScript.js
Cheers!

nodejs merge array

Im doint some nodejs fiddling with blogposts from wordpress and geotagging of theese posts. I have integrated geolite into nodejs and from wordpress i get the client id. Here is what my nodejs code looks like for now.
native.on('data',
function(data)
{
//console.log(data)
listener.sockets.emit('notification', data);
jsonstring = JSON.parse(data)
var ip = jsonstring.clientip
var geo = geoip.lookup(ip);
console.log(ip);
console.log(geo);
listener.sockets.emit('geodata', geo);
}
);
As you can see the lat / long is sent seperate from the json encoded data to the socket.
I want to merge the lat / long into "data" and sent is as 1 object. I cant figure out how to do this. i Hope someone can help me out with this.
An expando/ad-hoc property or two should suffice:
listener.sockets.emit('notification', data);
jsonstring = JSON.parse(data)
var ip = jsonstring.clientip
var geo = geoip.lookup(ip);
jsonstring.geo = geo;
// or
jsonstring.lat = geo.lat;
jsonstring.lng = geo.lng;
Add the geo information as another property of your parsed data object before emitting it:
native.on('data',
function(data)
{
var obj = JSON.parse(data)
obj.geo = geoip.lookup(obj.ip);
listener.sockets.emit('notification', JSON.stringify(obj));
}
);
You can also use
listener.sockets.emit('notification', data);
jsonstring = JSON.parse(data)
var ip = jsonstring.clientip
var geo = geoip.lookup(ip);
jsonstring['geo'] = geo;
to append the data in jsonstring
[ ] will be more helpful when we have dynamic key values

Binding JSON string to ListView in Metro apps?

I have a metro application(HTML5 & WinJS) in which am trying to display service data . Actually here am retrieving JSON data from my service but am unable to bind this data into listview . Anyone give me some working example.
Thank you.
You can use the WinJS.xhr() for this. You can read more about it on this link https://msdn.microsoft.com/pt-br/library/windows/apps/br229787.aspx and here is an example:
var path = "data/file.json";
function getData(path) {
WinJS.xhr({ url: path }).then(
function (response) {
var json = JSON.parse(response.responseText);
// Since this is an asynchronous function, you can't
// return the data, so you can:
// 1) retrieve the data to a namespace once the app loads.
var list = new WinJS.Binding.List(json);
Somenomespace.data = list;
// 2) or do all the binding inside the function.
var listView = document.getElementById("listViewID");
listView.winControl.itemDataSource = list.dataSource;
});
}
If you use the built in JSON.parse(jsonString) function you can loop through the content using a normal for loop as it then is a normal object and add it as usuall. Just remember to process or render the data.
Her is an example from code i had in a search page using listview:
var response = JSON.parse(data) ;
var originalResults = new WinJS.Binding.List();
for (x in response) {
originalResults.push(response[x]);
}
this.populateFilterBar(element, originalResults);
this.applyFilter(this.filters[0], originalResults);