okutil_http_get
Description
get a string from a web service
Syntax
int okutil_http_get( LPCSTR lpcszURL, string * pstrResult, int nConnectTimeout = 0, int nResponseTimeout = 0 )
Parameters
- lpcszURL
- [input] the web service URL
- pstrResult
- [output] result from the call
- nConnectTimeout
- [input] optional, in seconds
- nResponseTimeout
- [input] optional, in seconds
Return
0 if succcess, otherwise an negative error code
Examples
EX1
void test_rest_service()
{
string result;
int err = okutil_http_get("http://services.groupkt.com/state/get/IND/UP", &result);
if( err )
printf("Error %d\n", err);
else //no err
{
printf("%s\n", result);
Tree tr;
JSON.FromString(tr, result);
out_tree(tr);
}
}
EX2
//assume default new project with active book
void so2()
{
string url = "http://api.openweathermap.org/pollution/v1/so2/0.0,10.0/2016Z.json?appid=f80146dfac8f9ecbc2bbcc1cf97465cb";
string strJS;
int err = okutil_http_get(url, &strJS);
if( err != 0 )
return;
Tree tr;
JSON.FromString(tr, strJS);
Worksheet wks = Project.ActiveLayer();
Column col1 = wks.Columns(0);
Column col2 = wks.Columns(1);
vectorbase &v1 = col1.GetDataObject();
vectorbase &v2 = col2.GetDataObject();
JSON.TreeToVector(tr.data, v1, "pressure");
JSON.TreeToVector(tr.data, v2, "value");
}
EX3
#include <ocu.h> //for ocu_url_encode
void yy()
{
string url ="https://query.yahooapis.com/v1/public/yql?q=";
string strSearch = "select item.condition from weather.forecast where woeid = 2487889";
ocu_url_encode(&strSearch);
url += strSearch;
url += "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
string strJS;
int err = okutil_http_get(url, &strJS);
if( err != 0 )
return;
Tree tr;
JSON.FromString(tr, strJS);
out_tree(tr);
}
Remark
See Also
- JSON::FromString
- JSON::TreeToVector
header to Included
origin.h
Reference
|