黑莓屏幕更新与新数据

时间:2011-09-29 08:45:37

标签: blackberry http-get

我正在开发Blackberry应用程序。我在屏幕上有一张地图。我想用我从我的网络服务获得的新数据刷新地图的数据。我在Thread中使用BlockingSenderDestination。当我请求“获取数据”时,它返回新数据。没问题。我正在使用invokelater函数来调用我的maprefresh函数并传递参数,但我得到了非常规的错误。

有任何建议可以解决我的问题或更好的方法吗?

这是我的代码:

  public class MyMainScreen extends MainScreen  {
    RichMapField map;
    MyClassList _myclassList;
  private String _result2t;

    public MyMainScreen(JSONArray jarray)
    {


  map = MapFactory.getInstance().generateRichMapField();
        MapDataModel mapDataModel = map.getModel();

         JSONObject json = null;
            boolean getdata=false;
            for (int i=0;i<jarray.length();i++)
            {

                try
                {
            json=jarray.getJSONObject(i);
                getdata=true;
                }
                catch(Exception e)
                {

                }

                if(getdata)
                {
                    try
                    {                       
                     double  lat = Double.valueOf(json.getString("LATITUDE")).doubleValue();
                 double  lng  = Double.valueOf(json.getString("LONGITUDE")).doubleValue();               
                 String myclassdata= json.getString("myclassdata").toString(); 
                 MyClass ben =  new MyClass(myclassdata);                
                 _myclassList.addElement(ben);      
                 MapLocation termimapitem = new MapLocation( lat, lng, "","");
                 mapDataModel.add((Mappable)termimapitem,"1");
                    }

                    catch(Exception e)
                    {
                        //mesajGoster("Hatalı Veri");
                    }

                }
                else
                {
                    //mesajGoster("Listeye Eklenemedi");
                }
            }
     }


    private void GetTerminals(String companyNo){
        final String companyNoR= companyNo;
        Thread t = new Thread(new Runnable() 
        {
            public void run()
            {
                Message response = null;
                String uriStr = "http://webservice";                
                BlockingSenderDestination bsd = null;
                try
                {
                    bsd = (BlockingSenderDestination)
                               DestinationFactory.getSenderDestination
                                   ("o", URI.create(uriStr));
                    if(bsd == null)
                    {
                        bsd =
                          DestinationFactory.createBlockingSenderDestination
                              (new Context("o"),
                               URI.create(uriStr)
                               );
                    }

                    response = bsd.sendReceive();

                    if(response != null)
                    {   
                        BSDResponse(response,companyNoR);
                    }
                }
                catch(Exception e)
                {
                }
                finally
                {
                    if(bsd != null)
                    {
                        bsd.release();
                    }
                }
            }

        });
        t.start();  

    }

    private void BSDResponse(Message msg,final String companyNo)
    {

        if (msg instanceof ByteMessage)
        {
            ByteMessage reply = (ByteMessage) msg;
            _result2t = (String) reply.getStringPayload();
        } else if(msg instanceof StreamMessage)
        {
            StreamMessage reply = (StreamMessage) msg;
            InputStream is = reply.getStreamPayload();
            byte[] data = null;
            try {
                data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
            } catch (IOException e) {
                // process the error
            }
            if(data != null)
            {
                _result2t = new String(data);
            }
        }
        try {
                final JSONArray jarray= new JSONArray(_result2t);               
          final String username=_userName;
          UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                         Dialog.alert("The Toolbar i");
                        Yenile(jarray);
                    }

                });

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



    private void Yenile(JSONArray jarray){
        MapDataModel mapDataModel = map.getModel();
        mapDataModel.remove("1");
        map.getMapField().update(true);
        _terminalList = new TerminalList();
        map= MapFactory.getInstance().generateRichMapField();
        MapDataModel mapDataModel = map.getModel();
        JSONObject json = null;
        boolean getdata=false;
        for (int i=0;i<jarray.length();i++)
        {

            try
            {
        json=jarray_terminaller.getJSONObject(i);
            getdata=true;
            }
            catch(Exception e)
            {

            }

            if(getdata)
            {
                try
                {                       

                 double  lat = Double.valueOf(json.getString("LATITUDE")).doubleValue();
                 double  lng  = Double.valueOf(json.getString("LONGITUDE")).doubleValue();

                 String myclassdata= json.getString("myclassdata").toString(); 
                 MyClass ben =  new MyClass(myclassdata);                
                 _myclassList.addElement(ben);      
                 MapLocation termimapitem = new MapLocation( lat, lng, "","");
                 mapDataModel.add((Mappable)termimapitem,"1");
                }

                catch(Exception e)
                {
                    //mesajGoster("Hatalı Veri");
                }

            }
            else
            {
                //mesajGoster("Listeye Eklenemedi");
            }
        }


    }
}

2 个答案:

答案 0 :(得分:0)

要刷新屏幕:请执行以下操作:

public class LoadingScreen extends MainScreen{
LoadingScreen()
{
    createGUI();
}
public void createGUI()
{
    //Here you write the code that display on screen;
}}

我们知道这是创建屏幕的实际方式;

现在,如果你想刷新屏幕,如下所示:

deleteAll();
invalidate();
createGUI();//here it creates the screen with new data.

而不是写入 InvokeLater 方法,而不是在Thread.sleep(10000)之后在run方法中编写上面三行;

如果您对stackOverFlow聊天室名称“黑莓生活”有任何疑问,请澄清您和我们的疑虑。

答案 1 :(得分:0)

我找到了解决问题的方法。

获取数据后,我通过新的运行方法发送它:

UiApplication.getUiApplication().invokeLater(new Runnable() {
 public void run() {
         MyFunction(jarray);
       }});

但我需要与主线程同步。所以解决方案:

UiApplication.getUiApplication().invokeLater(new Runnable() {
              public void run() {
                     synchronized(Application.getEventLock()) {
                         Yenile(jarray);
                     }

                }

            });