Expand my Community achievements bar.

SOLVED

HTTPService and JSON

Avatar

Level 2
I am trying get dynamic data into my charts using
mx:HTTPService to get a JSON. I have used the flex 3 help and
combed the forums to get this far but i am now stuck.

here is my code


quote:





<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
layout="absolute"
viewSourceURL="srcview/index.html"preinitialize="srv.send();" >



<mx:Script>

<![CDATA[



import mx.collections.ArrayCollection;

import mx.rpc.events.ResultEvent;

import com.adobe.serialization.json.JSON;

import mx.controls.Alert;

import mx.utils.ObjectUtil;



[Bindable]

private var expensesAC:ArrayCollection = new
ArrayCollection( [

{ time: "1-24 19:00", cal: 2000, tva: 1500 },

{ time: "1-24 20:00", cal: 1000, tva: 200 } ]);



private function onJSONLoad(event:ResultEvent):void{

Alert.show("onJSONLoad called!");

var myData:String = String(event.result.DATA);

Alert.show("data worked");



// Alert.show(myData);

// myText.text = myData;

// var ary:Array = (JSON.decode(myData) as Array);

// var dp:ArrayCollection = new ArrayCollection(ary.data);

// linechart.dataProvider = dp;

}



private function failed():void{

Alert.show("Fail!!");

}



]]></mx:Script>



<!-- HTTP SERVICE -->

<mx:HTTPService id="srv" resultFormat="text"
showBusyCursor="true"

url="-MySever-/readings.jsp"

result="onJSONLoad(event)" fault="failed()" />





<!-- CHART STUFF BELOW -->

<mx:TextArea id="myText" y="310" width="600" height="250"
/>

<mx:Panel title="ISO Hourly Demand" height="300"
width="600" layout="horizontal" >

<mx:LineChart id="linechart" showDataTips="true"
dataProvider="{expensesAC}">

<mx:horizontalAxis>

<mx:CategoryAxis categoryField="time" />

</mx:horizontalAxis>

<mx:series>

<mx:LineSeries yField="cal" form="curve" displayName="
Calforina" />

<mx:LineSeries yField="tva" form="segment"
displayName="TVA" />

</mx:series>

</mx:LineChart>

</mx:Panel>

</mx:Application>





So when i run this i get the Alert "onJSONLoad called!" but
not the "data worked".



when i run the service in my browser with firebug i get a
response that looks like so




quote:





{

"CALIFORNIA" : {

demand : [ <list of ints> 40, 41,42,43, ect ],

baseline : [ <list of ints> 40, 41,42,43, ect ],

numReadings : [ <list of ints> 40, 41,42,43, ect ]

},

"ERCOT" : {

demand : [ <list of ints> 40, 41,42,43, ect ],

baseline : [ <list of ints> 40, 41,42,43, ect ],

numReadings : [ <list of ints> 40, 41,42,43, ect ]

}, ect...







so i seems fairly obvious that the problem lies in the "var
myData:String = String(event.result.DATA);" line. To be honest i
pretty much took this line right out of the help file so i have no
idea what is wrong with it. help?



Thanks and good day
1 Accepted Solution

Avatar

Correct answer by
Level 2
Ok got it



var myData:String = String(event.result.DATA);

should be

var myData:String = String(event.result);



now its just a matter of taking our different arrays and
putting them into the collection

View solution in original post

1 Reply

Avatar

Correct answer by
Level 2
Ok got it



var myData:String = String(event.result.DATA);

should be

var myData:String = String(event.result);



now its just a matter of taking our different arrays and
putting them into the collection