2013年5月22日星期三

ADF_229:ADF Mobile 11.1.2.4 Samples 介绍(15):Weather3

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

Weather3与Weather2基本实现相同,除了点击查询按钮时,实现了异步调用。

知识点:

1. 新增ForecastWorker.java,作为多线程任务类

package mobile;

import oracle.adfmf.framework.api.AdfmfJavaUtilities;

public class ForecastWorker implements Runnable {
    CityInformation ci = null;
    String zip = "";

    public ForecastWorker() {
        super();
    }

    public ForecastWorker(CityInformation ci, String zip) {
        this.ci = ci;
        this.zip = zip;
    }

    public void run() {
        ci.retrieveForecastAsync(zip);
        AdfmfJavaUtilities.flushDataChangeEvent();
    }
}

注意这行代码:AdfmfJavaUtilities.flushDataChangeEvent();,有了这行代码,界面才能刷新。

2. CityInformation.java中的相关方法

    public synchronized boolean retrieveForecast(String zip) {
        // First lets clear the cityForecast
        cityForecast.setSuccess((Boolean)Boolean.FALSE);
        cityForecast.setResponseText((String)"Running...");
        cityForecast.setCity((String)"");
        cityForecast.setState((String)"");
        cityForecast.setWeatherStationCity((String)"");
        cityForecast.clearForecast();
     
        ForecastWorker fw = new ForecastWorker(this, zip);
        Thread t = new Thread(fw);
        t.start();
     
        return true;
    }
 
    public synchronized boolean retrieveForecastAsync(String zip) {
        // Before we get any forecast, we get the WeatherInfo if it's not retrieved yet
        weatherInfo.retreiveWeatherInfo();

        boolean ret = false;

        List pnames = new ArrayList();
        List params = new ArrayList();
        List ptypes = new ArrayList();

        pnames.add("ZIP");
        ptypes.add(String.class);
        params.add(zip);

        try {
            // This calls the DC method and gives us the Return
            GenericType result =
                (GenericType)AdfmfJavaUtilities.invokeDataControlMethod("WeatherDC", null, "GetCityForecastByZIP",
                                                                        pnames, params, ptypes);

            // This will give us the CityForeCast object from the result
            GenericType cfgt = (GenericType)result.getAttribute(0);

            // Read the attributes from the GenericType returend from the getCityForecastByZip call          
            cityForecast.setSuccess((Boolean)cfgt.getAttribute("Success"));
            cityForecast.setResponseText((String)cfgt.getAttribute("ResponseText"));
            cityForecast.setCity((String)cfgt.getAttribute("City"));
            cityForecast.setState((String)cfgt.getAttribute("State"));
            cityForecast.setWeatherStationCity((String)cfgt.getAttribute("WeatherStationCity"));

            // This will give us the ForecastResult which is a collection of Forecast objects
            GenericType frgt = (GenericType)cfgt.getAttribute("ForecastResult");


            // fcgt is a collection of Forecast objects, get all those in a loop
            int count = frgt.getAttributeCount();
            for (int i = 0; i < count; i++) {
                // Get each individual WeatherDescription object
                GenericType fgt = (GenericType)frgt.getAttribute(i);

                // Now make a real WeatherDescription java object out of this GenericType
                Forecast f = (Forecast)GenericTypeBeanSerializationHelper.fromGenericType(Forecast.class, fgt);
                f.weatherInfo = weatherInfo;

                // Now get the Temperature subobject
                GenericType tempgt = (GenericType)fgt.getAttribute("Temperatures");

                // Now set the high and low temps
                f.setDaytimeHigh((String)tempgt.getAttribute(0));
                f.setMorningLow((String)tempgt.getAttribute(1));
                // Now add this to our holder of all WeatherDescriptions
                cityForecast.addForecast(f);
             
            }
            ret = true;
        } catch (AdfInvocationException e) {
            Trace.log(Utility.ApplicationLogger, Level.SEVERE, CityInformation.class, "retrieveForecastAsync",
                      ">>>>>> AdfInvocationException=" + e.getMessage());
            AdfException ex = new AdfException("Error Invoking Web Service.  Please try later", AdfException.WARNING);
            throw ex;

        } catch (Exception e2) {
            Trace.log(Utility.ApplicationLogger, Level.SEVERE, CityInformation.class, "retrieveForecastAsync",
                      ">>>>>> Exception=" + e2.getMessage());
            AdfException ex = new AdfException("Error Invoking Web Service.  Please try later", AdfException.WARNING);
            throw ex;
        }
        return ret;
    }

3. 部署,运行
(1)可以看到点击查询按钮后,会马上进入到下一个页面,但是数据并没有完全得到。
这时,并不会Block用户可以做其他事情。


(2)获取数据后,界面自动刷新。



参考文献:
1. https://blogs.oracle.com/mobile/entry/web_service_example_part_3

没有评论: