Voorbeeld programmas
Op deze pagina vind je een aantal handige Progress/openedge voorbeeld procedures icm webclient / json .
- Ophalen van een bestand
- Ophalen landcode van ipadres
- Converteer json datetimestring (facebook) naar openedge datetime
- Bepaal geo coordinaten van adres via openedge icm google maps

Het ophalen van een willekeurige url / bestand via openedge van internet
Via WebClient van .NET kun je eenvoudig een bestand van internet naar je toe halen, zie onderstaand voorbeeld voor het naar je toehalen van de foreach mainpage

/* download file from internet */

USING System.Net.*.

def var oWebClient as CLASS WebClient no-undo.
def var cUrl as char no-undo.
def var cDownload as longchar no-undo.


oWebClient = new Webclient().
owebClient:Headers:Add("Charset", "text/html; charset=UTF-8").
oWebclient:Headers:Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ).

cUrl = 'http://www.foreach.nl' .  /* file to download */
cDownload = oWebclient:DownloadString (cUrl) no-error.

/* save as local file
copy-lob from cDownload to file ('c:/temp/mainpage.html') no-convert.
*/

message string(cDownload) view-as alert-box.

delete object owebclient.

Bepaal de landcode van een ipadres met openedge en ip-api.com
Via ip-api.com kun je de countrycode van een ipadres achterhalen.

/* get country code via ip-api */

USING System.*.
USING System.Web.*.
USING System.Net.*.
USING Progress.Json.ObjectModel.*.
    
def var oWebClient as CLASS WebClient no-undo.
def var oJsonParser    as ObjectModelParser NO-UNDO.
def var cDownload as longchar no-undo.
def var cIpAddress as char no-undo format "x(20)" init "64.233.191.255". 
def var cCountry as char no-undo.

define variable jsonMessage   AS JsonObject NO-UNDO.

oJsonParser = NEW ObjectModelParser( ).
                
oWebClient = new Webclient().
owebClient:Headers:Add("Charset", "text/html; charset=UTF-8").
oWebclient:Headers:Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ).
  
cDownload = oWebclient:DownloadString ( 'http://ip-api.com/json/' + cIpAddress ) no-error.

JsonMessage = CAST(oJsonParser:Parse(cDownload), JsonObject) no-error.
   
if jsonMessage:has("countryCode")
then cCountry = JsonMessage:GETCHARACTER( "countryCode" ).
else cCountry = "??".

delete object oWebClient.
delete object JSONMESSAGE.
delete object oJsonParser.

message cCountry.

Converteer json (facebook) datetimestring to openedge datetime / datetime-tz

/* convert json (facebook) datetimestring to progress datetime-tz */
            
function string2datetimetz RETURNS datetime-tz (INPUT cString AS CHAR):
  /* 2012-11-09T23:00:00+0100 gekopieerd van fb_slurp */
  
  def var dDateTimeTz as datetime-tz no-undo. 
  def var cTmp as char  no-undo.
  def var dDate as date no-undo.
  def var ms as int     no-undo.
  def var tz as int     no-undo.
  def var j  as int     no-undo.
  
  cTmp = cString.
  if ctmp <> ''  /* 2012-11-09T23:00:00+0100 met de hand converteren, progress kent wel iso-date, maar geen weg terug :( */
  then do:
    assign dDate = ?
           ms    = 0
           tz    = 0.
               
    dDate = date( int( substring( cTmp,6,2) ),  int( substring( cTmp,9,2) ), int( substring( cTmp,1,4) ) ) no-error.
    j = index( ctmp, 'T' ).
    if j > 0 
    then cTmp = substring( cTmp, J + 1 ).
    else assign cTmp = ''
                dDatetimetz = datetime-tz( ddate, 0 ).

        
    if ctmp > '' /* 23:00:00+0100 */
    then do:
      ms = int( substring( cTmp, 1, 2 )) * 60 * 60 * 1000.
      ms = ms + int( substring( cTmp, 4, 2 )) * 60 * 1000.
      ms = ms + int( substring( cTmp, 7, 2 )) * 1000.
    
      if length(cTmp) > 8
      then do:
        cTmp = substring( ctmp, 9 ).
        tz = int( cTmp ) * 60.
		
		/* tz kan neg en pos zijn */
        if tz <> 0 then tz = tz / 100.
      end.
         
      dDatetimetz = datetime-tz( ddate, ms, tz ) no-error.
	     
      
    end.
  end.
  return dDateTimeTz.
end.

def var dDatetime as datetime no-undo.
def var dDateTimeTz as datetime-tz no-undo. 
def var cStart as char no-undo.

cStart = '2015-12-04T23:00:00+0000'.

dDatetimeTZ = string2datetimetz( cStart ).
dDateTime = string2datetimetz(CSTART ) .


message dDateTime   skip
    dDatetimeTZ
    view-as alert-box.



Bepaal de geo coordinaten in openedge via google maps

/* get geo code via google maps */

USING System.Net.*.
USING Progress.Json.ObjectModel.*.


def var cLong as char no-undo.
def var cLat as char no-undo.
def var cAdres as char no-undo init "europlaan 22+amsterdam+nl". /* RAI amsterdam */

run ip_getgeocode( input cAdres, output cLat, Output cLong ).

message cLat cLong view-as alert-box.
  

procedure ip_getgeocode:
  def  input parameter cAddress as char no-undo.
  def  output parameter dLat    as dec no-undo.
  def  output parameter dLong   as dec no-undo.
  
  def var cDownload    as longchar   no-undo .
  def var geoArray     as JsonArray  no-undo.
  def var jsonMessage  as JsonObject no-undo.
  def var jsonlocation as jsonobject no-undo.
  
  def var cUrl as char no-undo.   
  def var cTmp as char no-undo.
  
  def var oWebClient as CLASS WebClient no-undo.
  def var oJsonParser as ObjectModelParser no-undo.
  
  
  oWebClient = new Webclient().
  oJsonParser = new ObjectModelParser( ).
    
   /*owebClient:Headers:Add("CharSet", "UTF-8"). */
  owebClient:Headers:Add("Charset", "text/html; charset=UTF-8").
  oWebclient:Headers:Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ).

  
  cUrl = 'http://maps.googleapis.com/maps/api/geocode/json?address='
     + replace( cAddress, ' ', '%20' ) + '&sensor=false'.
  cDownload = oWebclient:DownloadString (cUrl) no-error.
  if error-status:error
  then run ip_loggooglemapserror(  cUrl + ' ' + ERROR-STATUS:GET-MESSAGE(1) ).
  else copy-lob from cDownload to file ( session:temp-directory + 'lastgeocode.json') no-convert no-error.
  
  JsonMessage = CAST(oJsonParser:Parse(cDownload), JsonObject).
  geoArray = jsonmessage:GETJSONARRAY("results").
  


  if geoarray:Length >= 1
  then do:
    jsonMESSAGE = geoarray:GETJSONOBJECT(1).
    if jsonMESSAGE:has( "geometry" ) 
    then do:
      if jsonMESSAGE:GetJsonObject( "geometry" ):has( "location" ) 
      then do:
        cTmp = session:numeric-format.
        session:numeric-format = 'American'.
        
        jsonlocation = jsonMESSAGE:GetJsonObject( "geometry" ):getJsonObject( "location" ).
        dLat  = jsonlocation:GetDecimal("lat") .
        dLong = jsonlocation:GetDecimal("lng" ).
        
        session:numeric-format = cTmp.        
      end.
    end.
  end.
  
  delete object oWebClient.
  delete object oJsonParser.
end.