2013年5月23日星期四

ADF_230:ADF Mobile 11.1.2.4 Samples 介绍(16):RESTDemo


开发运行环境:JDeveloper 11.1.2.4 + Android SDK r21.1

RESTDemo演示了如何使用REST风格的Web Service。
这里展示了两种使用方法:REST-XML和REST-JSON,去调用一个公共的Web Service:http://freegeoip.net/,获取当前机器的地理位置。
REST-XML使用了一个XSD,并创建了一个URL Service Data Control去访问结构化的数据,UI层直接绑定到这个DC上。
REST-JSON使用了一个帮助类:RESTServiceAdapter去获取Web Service的URL connection,然后使用帮助类:JSONSerializationHelper解析返回结果(非结构化的数据)。UI层是间接绑定到一个Managed Bean生成的DC。



1. RESTJSONBean.java 代码

package mobile;

import oracle.adfmf.dc.ws.rest.RestServiceAdapter;
import oracle.adfmf.framework.api.JSONBeanSerializationHelper;
import oracle.adfmf.framework.api.Model;
import oracle.adfmf.java.beans.PropertyChangeListener;
import oracle.adfmf.java.beans.PropertyChangeSupport;

public class RESTJSONBean {

    private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public RESTJSONBean() {
    }

    private String searchIp = "oracle.com";
    private String jsonResponse = "";
    private RESTJSONResponse response = null;

    public void setSearchIp(String searchIp) {
        String oldSearchIp = this.searchIp;
        this.searchIp = searchIp;
        propertyChangeSupport.firePropertyChange("searchIp", oldSearchIp, searchIp);
    }

    public void addPropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.removePropertyChangeListener(l);
    }

    public String getSearchIp() {
        return searchIp;
    }

    public void setJsonResponse(String response) {
        String oldResponse = this.jsonResponse;
        this.jsonResponse = response;
        propertyChangeSupport.firePropertyChange("jsonResponse", oldResponse, response);
    }

    public String getJsonResponse() {
        return jsonResponse;
    }

    public void setResponse(RESTJSONResponse response) {
        RESTJSONResponse oldResponse = this.response;
        this.response = response;
        propertyChangeSupport.firePropertyChange("response", oldResponse, response);
    }

    public RESTJSONResponse getResponse() {
        return response;
    }

    public void loadData() {
        RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();

        // Clear any previously set request properties, if any
        restServiceAdapter.clearRequestProperties();

        // Set the connection name
        restServiceAdapter.setConnectionName("GeoIP");

        // Specify the type of request
        restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET);

        // Specify the number of retries
        restServiceAdapter.setRetryLimit(0);

        // Set the URI which is defined after the endpoint in the connections.xml.
        // The request is the endpoint + the URI being set
        restServiceAdapter.setRequestURI("/json/" + getSearchIp());

        setJsonResponse("");

        // Execute SEND and RECEIVE operation
        try {
            // For GET request, there is no payload
            setJsonResponse(restServiceAdapter.send(""));
            
            // Now create a new RESTJSONResponse object and parse the JSON string returned into this class
            RESTJSONResponse res = new RESTJSONResponse();
            res = (RESTJSONResponse)JSONBeanSerializationHelper.fromJSON(RESTJSONResponse.class, getJsonResponse());
            setResponse(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

代码中的连接"GeoIP" 定义来自于URL Connection:



参考文献:
1. https://blogs.oracle.com/mobile/entry/adf_mobile_rest_json_xml
2. http://www.youtube.com/watch?v=HOesFpjBz2M
3. http://biemond.blogspot.com/2012/10/using-json-rest-in-adf-mobile.html

没有评论: