//创建新线程处理联网动作,并更新屏幕
(new Thread(){
public void run(){
UiApplication.getUiApplication().invokeLater(
new Runnable(){
public void run(){
try {
String respBody = Util.getViaHttpConnection(url);//url是requestToken方法中的变量,必须声明成final;
OAuthMessage responseMessage = new OAuthMessage();
responseMessage.parseResponseStringForToken(respBody);
RequestToken token=new RequestToken(responseMessage.getToken(), responseMessage.getTokenSecret());
TextField text = new TextField();
text.setText(respBody);
screen.add(text);//requestToken参数,必须声明成final
} catch (OAuthBadDataException e) {
System.out.println("debug>" + e.toString() + ";");
}catch (Exception e){
System.out.println("debug>" + e.getMessage());
}
};
}
);
};
}).start();
把这节代码放在一个方法或 是另一个方法中,测试发现主线程还是lock了,最后得知,内部类是会暂停主类的;
需要修改成
---------
把下页代码放到类的内部,因为打算多个地方调用,回调也不一样,所以使用字符比较方式,因为平台不支持getMethod();
//线程子类,必须使用类创建,使用匿名类时,还是会暂停主类
private class newThread extends Thread{
newThread(Consumer caller, String run, String call, Object obj, Object screen){
this.run = run;
this.call = call;
this.caller = caller;
this.obj = obj;
this.screen = screen;
}
public void run(){
startCall();
}
private Object startRun(){
if ( run.equals("getUrl") ){
return Util.getViaHttpConnection((String) obj);
}else{
return null;
}
}
private void startCall(){
Object reObj = startRun();
if ( call.equals("requestToken") ){
caller.requestTokenCall((String)reObj, (ReaderScreen) screen);
}
}
private Consumer caller = null;
private String run = null;
private String call = null;
private Object obj = null;
private Object screen = null;
}
-----
启动线程
new newThread(this, "getUrl", "requestToken", url, screen).start();
----
回调方法,更新ui
private void requestTokenCall(final String respBody, final ReaderScreen screen){
UiApplication.getUiApplication().invokeLater(
new Runnable(){
public void run(){
try {
System.out.println("tip>respBody " + respBody);
OAuthMessage responseMessage = new OAuthMessage();
responseMessage.parseResponseStringForToken(respBody);
RequestToken token=new RequestToken(responseMessage.getToken(), responseMessage.getTokenSecret());
TextField text = new TextField();
text.setText(respBody);
screen.add(text);//requestToken参数,必须声明成final
} catch (OAuthBadDataException e) {
System.out.println("debug>" + e.toString() + ";");
}catch (Exception e){
System.out.println("debug>" + e.getMessage());
}
};
}
);
}
------------------
方案二,此方案更加合理点,每个线程维护一个runnable接口实现
// 请求token
public void requestToken(ReaderScreen screen){
OAuthMessage requestMessage = new OAuthMessage();
requestMessage.setRequestURL(REQUEST_TOKEN_URL);
requestMessage.setConsumerKey(config.getKey());
if (OAUTH_COMSUMER_CALLBACK != null)
requestMessage.setCallback(OAUTH_COMSUMER_CALLBACK);
requestMessage.createSignature(oauth_comsumer_method, config.getSecret());
System.out.println("debug>endpoint:" + REQUEST_TOKEN_URL + ";");
final String url=REQUEST_TOKEN_URL + "?" + requestMessage.convertToUrlParameters();
System.out.println("debug>visit for url = endpoint + requestMessage.convertToUrlParameters() : "+url + ";");
new Thread(new requestTokenRun(url, screen)).start();//启动线程
}
//线程子类,必须使用类创建,使用匿名类时,还是会暂停主类
private class requestTokenRun implements Runnable{
requestTokenRun(String url, ReaderScreen screen){
this.url = url;
this.screen = screen;
}
public void run(){
try {
final String respBody = Util.getViaHttpConnection(url);//必须声明final invokeLater才能使用;
System.out.println("tip>respBody " + respBody);
OAuthMessage responseMessage = new OAuthMessage();
responseMessage.parseResponseStringForToken(respBody);
RequestToken token=new RequestToken(responseMessage.getToken(), responseMessage.getTokenSecret());
UiApplication.getUiApplication().invokeLater(
new Runnable(){
public void run(){
TextField text = new TextField();
text.setText(respBody);
screen.add(text);
};
}
);
}catch (Exception e){
System.out.println("tip>requestTokenRun " + e);
}
}
private String url = null;
private ReaderScreen screen = null;
}
标签:String,主类,类会,screen,private,url,respBody,线程,new
From: https://blog.51cto.com/u_252283/6180650