Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Is there an example for DataSource.transformResponse?

    I customized RestDataSource to support our REST service that's successfully to post the correct request with right data but failed at response.
    Our rest service will return a map object on JSON format for any REST request. So far, there're two problem as follows:
    1.callback is never invoked after requesting a "GET" and server's response is no problem
    2.DSResponse has nothing about response data. I think we need to override the method of DataSource.transformResponse but no example code anywhere
    Any hint will be appreciated. TIA!

    #2
    for problem 2
    You have to override transformResponse in your DataSource. Object data has the response from the server

    Code:
    @Override
    	protected void transformResponse(DSResponse response, DSRequest request, Object data) {
    		
    		if (request.getOperationType().equals(DSOperationType.REMOVE))
    		{
    			if(XMLTools.selectString(data,"/XDocument/XStatus").equalsIgnoreCase("Failed"))
    			{					
    				response.setStatus(-1);
    				Map<String, String> mpErrors = new HashMap<String, String>();
    				mpErrors.put("XErrorDesc", XMLTools.selectString(data,"/XDocument/XErrorDesc"));
    				response.setErrors(mpErrors);
    			}
    			
    		}
    		
    		super.transformResponse(response, request, data);
    
        }
    I am using REST services that return XML responses like below.

    Code:
    <XDocument>
        <XStatus>Failed</XStatus>
        <XAction>GET</XAction>
        <XErrorDesc>orgID: Invalid</XErrorDesc>
        <XService>User</XService>
    </XDocument>

    Comment


      #3
      Thank you, ADA.
      My problem is how to transform response as JSON object. You see, here's my json format from the server:
      Code:
      {    
          response:{
             flag:0,
             errorcode:0,
             screen:CONTACT,
             object:[
                 {field1:"value", field2:"value"},
                 {field1:"value", field2:"value"},
                 ... 75 total records ...
             ]
          }
       }
      What am I supposed to do with this kind of object?

      Comment


        #4
        The third parameter ("data") is your JSON structure as a JavaScriptObject. Use JSOHelper to access it's properties, or use XMLTools.selectObjects().

        Comment


          #5
          Thank you, Isomorphic.
          I get over the problem on the process of request-response by json format.
          But there's a new problem, how to transform the json array into the Record[] type which is taken by the method response.setData()?
          TIA!

          Comment


            #6
            JSOHelper + iteration.

            Comment


              #7
              Try

              Code:
              JavaScriptObject jsoArray = JSOHelper.html.convertMapToJavascriptObject(java.util.Map);
              Record[] records = Record.convertToRecordArray(jsoArray);

              Comment


                #8
                Thank you, smartgwt.dev. Your code is very helpful.

                Comment

                Working...
                X