9 Replies Latest reply on Apr 21, 2008 7:36 AM by tookiebunten

    writing HTTPService Error

    tookiebunten
      Hi all,

      I'm new to flex and actionscript and have been trawling Google trying to find an example but I'm having no luck. What I'm looking for is to return an error when my HTTPservice can't get a connection to the file its calling. Does have any ideas or can somebody point me towards an example that a complete novice will understand.

      Thanks
        • 1. Re: writing HTTPService Error
          derm116
          Hi,
          It's not too difficult to return an error from httpService!

          You could use the fault option in the <mx:HttpService tag, such as

          <mx:HTTPService id="service" method="GET" resultFormat="e4x"
          url="..."
          result="handleResult(event)"
          fault="handleFault(event)"
          />

          and then put in whatever you want in your handleFault function, such as

          public function handleFault(event):void {
          Alert.show("Connection error!");
          }

          A more correct way would be to import ResultEvent and FaultEvent, like so

          import mx.rpc.events.ResultEvent;
          import mx.rpc.events.FaultEvent;

          Have your fault in handleFault, such as

          public function httpFault(event:FaultEvent):void {
          Alert.show("Connection error!");
          }

          and have your result or whatever you want to do in resultEvent, such as

          public function handleResult(event:ResultEvent):void {
          Alert.show("Connection successful!");
          }

          Hope this helps,
          Derm
          • 2. Re: writing HTTPService Error
            tookiebunten Level 1
            Hi Derm,

            Thanks Derm for the help but I have another question

            This question is going to show how new and dumb I am when it comes to this stuff. Do I add all that to a button or will it automatically return an error?

            Thanks
            • 3. Re: writing HTTPService Error
              derm116 Level 1
              No, once the handleFault is set in the httpService tag as the fault

              fault="handleFault(event)"

              it'll handle it automatically if an error occurs!
              • 4. Re: writing HTTPService Error
                tookiebunten Level 1
                Derm

                I must be doing something as I'm not getting any error. The bottom left of the browser just says connecting to 145.1.whatever and just sits like that. Any ideas?

                Thanks for being patient.
                • 5. Re: writing HTTPService Error
                  derm116 Level 1
                  Post your code & I'll have a look at it!
                  • 6. Re: writing HTTPService Error
                    tookiebunten Level 1
                    No problem, please find attached my code. Hope its legible and not to much of a mess!!

                    Thanks
                    • 7. Re: writing HTTPService Error
                      derm116 Level 1
                      I just put the actionscript ahead of the httpservice tags and it seemed to work for me - well the fault came up!

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

                      <mx:Application
                      xmlns:mx=" http://www.adobe.com/2006/mxml"
                      pageTitle="Report Register ver 1.0"
                      borderStyle="solid"
                      backgroundGradientColors="[#FFFFFF, #FFFFFF]"
                      defaultButton="{submit}"
                      >

                      <!--Actionscripts-->
                      <mx:Script>
                      <![CDATA[
                      import mx.events.ValidationResultEvent;
                      import mx.validators.Validator;
                      import mx.controls.Alert;
                      import mx.rpc.events.FaultEvent;

                      public function httpFault(event:FaultEvent):void
                      {
                      Alert.show("Error! No Connection!")
                      }
                      private var imgBgrd:Class;
                      /* formats the DateField to DD-MMM-YY */
                      private function formatDate(date:Date):String {
                      return dmconv.format(date);
                      }
                      /* clears the text from inputs on the top of the form and resets error strings */
                      private function clearInputsTop():void
                      {
                      faultDescInput.text = "";
                      dateModInput.text = "";
                      repDescInput.text = "";
                      repCode1Input.text = "";
                      assignedToInput.text = "";
                      commentsInput.text = "";
                      reportedByInput.text = "";
                      faultDescInput.errorString = "";
                      dateModInput.errorString = "";
                      repDescInput.errorString = "";
                      repCode1Input.errorString = "";
                      assignedToInput.errorString = "";
                      commentsInput.errorString = "";
                      reportedByInput.errorString = "";
                      }
                      /* clears the text from inputs on the bottom of the form and resets error strings*/
                      private function clearInputsBottom():void{
                      repCode2.text = "";
                      repCode2.errorString = "";
                      }
                      /*validates the text input before sending to the server*/
                      private var vResult:ValidationResultEvent;
                      private function validateAndSubmit():void
                      {
                      //Validate the required fields
                      vResult = repCode1.validate();
                      if (vResult.type==ValidationResultEvent.INVALID)
                      return;
                      vResult = repDesc.validate();
                      if (vResult.type==ValidationResultEvent.INVALID)
                      return;
                      vResult = faultDesc.validate();
                      if (vResult.type==ValidationResultEvent.INVALID)
                      return;
                      vResult = dateMod.validate();
                      if (vResult.type==ValidationResultEvent.INVALID)
                      return;
                      vResult = assignedTo.validate();
                      if (vResult.type==ValidationResultEvent.INVALID)
                      return;
                      vResult = reportedBy.validate();
                      if (vResult.type==ValidationResultEvent.INVALID)
                      return;
                      vResult = comments.validate();
                      if (vResult.type==ValidationResultEvent.INVALID)
                      return;
                      //sends the data to the server
                      userRequest.send();
                      //visually alerts user that the table has been updated
                      Alert.show("Report Register updated!");
                      //clears input fields to start again
                      clearInputsTop();
                      //resets the focus to the top field
                      resetFocus();
                      }
                      private function handleValid(event:ValidationResultEvent):void
                      {
                      if(event.type==ValidationResultEvent.VALID)
                      submit.enabled = true;
                      else
                      submit.enabled = false;
                      }
                      //sets the focus to the first input on the form
                      private function resetFocus():void
                      {
                      focusManager.setFocus(repCode1Input);
                      }
                      //clears the datagrid
                      private function clearGrid():void
                      {
                      myDataGrid.dataProvider = null;
                      }
                      ]]>
                      </mx:Script>

                      <!--calls php file to send data to oracle database-->
                      <mx:HTTPService
                      id="userRequest"
                      url=" http://145.1.12.116/xampp/test/postData.php"
                      useProxy="false"
                      method="POST"
                      fault="httpFault(event)"
                      >
                      <mx:request xmlns="">
                      <ReportNumber>{repCode1Input.text}</ReportNumber>
                      <ReportName>{repDescInput.text}</ReportName>
                      <Date1>{dateModInput.text}</Date1>
                      <Users>{assignedToInput.text}</Users>
                      <Fault>{faultDescInput.text}</Fault>
                      <RUser>{reportedByInput.text}</RUser>
                      <Comments>{commentsInput.text}</Comments>
                      </mx:request>
                      </mx:HTTPService>
                      <!--calls php file to pull data from oracle database-->
                      <mx:HTTPService
                      id="userRequest2"
                      url=" http://145.1.12.116/xampp/test/getData.php"
                      useProxy="false"
                      method="POST"
                      fault="httpFault(event)"
                      >
                      <mx:request xmlns="">
                      <repCode2>{repCode2.text}</repCode2>
                      </mx:request>
                      </mx:HTTPService>

                      ...
                      • 8. Re: writing HTTPService Error
                        tookiebunten Level 1
                        Sorry Derm,

                        But there must be something else going on for me as that just does the same thing. Sits trying to get a connection when there isn't one. I've even unplug the network cable to make sure.

                        Thanks for your help, if you haven't got any other ideas I'll just have to plug away till I get it working.
                        • 9. Re: writing HTTPService Error
                          tookiebunten Level 1
                          It seems to be something in my firefox set up, I get the connection error if I run it in internet explorer. Strange? Anyone, any ideas?

                          Thanks