Return the correct value from a JavaME thread

橙三吉。 提交于 2019-12-10 12:15:53

问题


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;

}

回答1:


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.



来源:https://stackoverflow.com/questions/5549542/return-the-correct-value-from-a-javame-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!