从 JavaME 线程返回正确的值
我写了一个返回字符串的函数。并且有一个线程实现该函数,如下所示,我正在调用 [metaDataTrimmed = getMetaData(url);] 该函数并将返回值存储为字符串值。我的问题是该函数立即返回空字符串,这是它的初始值。我检查了我的功能是否正常工作。
所以我尝试使用 dirtybit 的 Thread.sleep() 方法,并尝试了 Thread.join() 。黑莓中是否有任何标准方法可以解决该问题,如果没有,解决该问题的好方法是什么?
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 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您使用
Thread
获得null
的原因是您没有等待线程执行结束。这与BlackBerry无关,而纯粹与Java有关。要解决这个问题,只需在单独的(与 UI)线程上执行整个任务(而不仅仅是其中的网络部分)即可。任务完成后,您需要更新 UI,然后只需使用UiApplication.getUiApplication().invokeLater(Runnable action)
模式。I think the reason you are getting
null
with aThread
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 useUiApplication.getUiApplication().invokeLater(Runnable action)
pattern.