从螺纹的JavaME返回正确的值 [英] Return the correct value from a JavaME thread

查看:136
本文介绍了从螺纹的JavaME返回正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写它返回一个字符串的函数。并有一个线程执行的功能,如遵循和我打电话[metaDataTrimmed =的getMetaData(URL);]此功能并返回值存储到一个字符串值。我的问题是,该函数立即返回空字符串,这是它的初始值。我检查了我的功能的正常使用。

所以,我尝试使用dirtybit一个视频下载()方法,也试过的Thread.join()。是否有任何黑莓标准方法来解决问题,如果没有什么是解决问题的好办法?

 私人字符串的getMetaData(最后弦乐mediaUrl){
         串metaDataT =;        螺纹metaThread =新主题(新的Runnable(){
            公共无效的run(){
            尝试{
                的StreamConnection的StreamConnection = NULL;
                的HttpConnection的HttpConnection = NULL;
                为InputStream的InputStream = NULL;
                的StreamConnection =(的StreamConnection)Connector.open(mediaUrl);
                的HttpConnection =(HttpConnection的)的StreamConnection;
                httpConnection.setRequestProperty(冰 - 元数据,1);
                INT则httpStatus = httpConnection.getResponse code();
                如果(==则httpStatus HttpConnection.HTTP_OK){                      字符串薄荷= httpConnection.getHeaderField(冰冷-metaint);
                      的InputStream = streamConnection.openInputStream();
                      INT长度=的Integer.parseInt(薄荷);
                      INT B = 0;
                      诠释计数= 0;
                      而(计数++ LT;长度){
                        B = inputStream.read();
                      }
                      INT metalength =((int)的B)* 16;
                     //如果(metalength< = 0){waitBitMetaData = 1;回报;}
                      中byte buf [] =新的字节[metalength]
                      inputStream.read(BUF,0,buf.length);
                      字符串元=新的String(BUF);
                      INT streamTilleIndex = metaData.indexOf(StreamTitle);
                     //如果(streamTilleIndex< = 0){waitBitMetaData = 1;回报;}
                      字符串streamTille = metaData.substring(streamTilleIndex);
                      INT eqindex = streamTille.indexOf('=');
                   //如果(eqindex< = 0){waitBitMetaData = 1;回报;}
                      INT colindex = streamTille.indexOf(';');
                   //如果(colindex< = 0){waitBitMetaData = 1;回报;}
                      字符串metaDatam = streamTille.substring(eqindex,colindex);
                      INT lengthOfMaetaDataM = metaDatam.length();
                      如果(lengthOfMaetaDataM< = 0){waitBitMetaData = 1;回报;}
                      metaDataParsed = metaDatam.substring(2,lengthOfMaetaDataM-2);
                      的System.out.println(metaDataParsed);
                      waitBitMetaData = 1;
                }            }
            赶上(例外五){
                的System.out.println(E);
                waitBitMetaData = 1;            }            }
             });
             metaThread.start();
 metaThread.start();
        //而(metaDataParsed.equals()|| waitBitMetaData == 0){
            //尝试{
            //视频下载(50);
            //}赶上(InterruptedException的E){
                //System.out.println(e);
            //}
        //}
        返回metaDataParsed;}


解决方案

我觉得你越来越发的原因是你没有等待线程执行结束。这是不相关的黑莓,但纯粹是与Java。为了解决这个只是做一个单独的(从UI)线程全年任务(不只是网络的一部分)。任务完成后,当你需要更新用户界面,那么只需使用 UiApplication.getUiApplication()的invokeLater(Runnable的动作)模式。

I wrote a function which returns a string. And there is a Thread implementation the function, like follows, and I am calling [metaDataTrimmed = getMetaData(url);] this function and store the return value to a string value. My problem is the the function immediately returns the null string, which is its initial value. And I checked my function works properly.

So I try for a Thread.sleep() method using a dirtybit and also tried for Thread.join(). Is there any standard method in BlackBerry to solve the problem, if not what is a good approach to solve the problem?

private String getMetaData(final String mediaUrl){  
         String  metaDataT = "";

        Thread metaThread = new Thread(new Runnable(){        
            public void run(){      
            try {
                StreamConnection streamConnection=null;
                HttpConnection httpConnection = null;
                InputStream inputStream =null;
                streamConnection=(StreamConnection)Connector.open(mediaUrl);
                httpConnection=(HttpConnection)streamConnection;        
                httpConnection.setRequestProperty("Icy-metadata", "1");                   
                int httpStatus=httpConnection.getResponseCode();
                if(httpStatus==HttpConnection.HTTP_OK){

                      String mint = httpConnection.getHeaderField("icy-metaint");
                      inputStream = streamConnection.openInputStream();     
                      int length= Integer.parseInt(mint);
                      int b = 0;    
                      int count =0;    
                      while(count++ < length){
                        b = inputStream.read();         
                      }          
                      int metalength = ((int)b)*16;
                     // if(metalength <= 0){waitBitMetaData = 1;return;}
                      byte buf[] = new byte[metalength];               
                      inputStream.read(buf,0,buf.length);              
                      String metaData = new String(buf);         
                      int streamTilleIndex =   metaData.indexOf("StreamTitle");
                     // if(streamTilleIndex <= 0){waitBitMetaData = 1;return;}
                      String streamTille = metaData.substring(streamTilleIndex);
                      int eqindex = streamTille.indexOf('=');
                   //   if(eqindex <= 0){waitBitMetaData = 1;return;}
                      int colindex = streamTille.indexOf(';');
                   //   if(colindex <= 0){waitBitMetaData = 1;return;}
                      String metaDatam =  streamTille.substring(eqindex, colindex);
                      int lengthOfMaetaDataM = metaDatam.length();
                      if(lengthOfMaetaDataM <= 0){waitBitMetaData = 1;return;}
                      metaDataParsed =metaDatam.substring(2, lengthOfMaetaDataM-2);
                      System.out.println(metaDataParsed);                         
                      waitBitMetaData = 1;
                }

            }
            catch (Exception e){
                System.out.println(e);
                waitBitMetaData = 1;

            }

            }
             });
             metaThread.start();
 metaThread.start();
        //while( metaDataParsed.equals("") || waitBitMetaData == 0){
            //try {
            //  Thread.sleep(50);
            //} catch (InterruptedException e) {
                //System.out.println(e);
            //}
        //}
        return metaDataParsed;

}

解决方案

I think the reason you are getting null with a Thread is you don't wait for the end of the thread execution. This is not related to BlackBerry, but is purely related to Java. To solve this just do the whole task (not just networking part of it) on a separate (from UI) thread. When upon task completion you'll need to update the UI, then just use UiApplication.getUiApplication().invokeLater(Runnable action) pattern.

这篇关于从螺纹的JavaME返回正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆