blackbarry jdk 明确提示,Connector.open方法会lock当前线程,这样就会导致正在运行程序像死机样,所以,必须新开进程进行这些操作,而主进程进行其它操作,如提示进度;
开线程代码
public static final String getViaHttpsConnection(String url){
String respBody = ""; // return empty string on bad things
getViaHttpsConnectionThread urlThread = new getViaHttpsConnectionThread(143, url);
urlThread.start();
respBody = urlThread.getRespBody();
return respBody;
}
//新开线程处理联网
private static class getViaHttpsConnectionThread extends Thread {
getViaHttpsConnectionThread(long minPrime, String url) {
this.minPrime = minPrime;
this.url = url;
}
public void run(){
int rc = 0;
HttpConnection c = null;
try {
System.out.println("debug>UTIL open url:" + url + ";");
String isPhone = "false";
if ( DeviceInfo.isSimulator() ) {//不是虚拟机,必须设置false
isPhone = "true";
System.out.println("tip>no in phone;");
}
String useWifi = "";
if ( WLANInfo.WLAN_STATE_CONNECTED == WLANInfo.getWLANState() ){//如果有wifi就优先使用,不存在时再让手机自动走通道
useWifi = ";interface=wifi";
System.out.println("tip>contect by wifi");
}
c= (HttpConnection) Connector.open(url + useWifi + ";deviceside=" + isPhone + ";ConnectionTimeout=30000", Connector.READ, true);
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Cache-Control", "no-store");
c.setRequestProperty("Connection", "close"); // not sure this is a good idea, but HTTP/1.0 might be less error-prone, some clients have trouble with chunked responses
System.out.println("debug>UTIL -- connection open;");
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
System.out.println("debug>UTIL -- got response code " + rc + ";");
// Get the length and process the data
int len = c.getHeaderFieldInt("Content-Length", 0);
System.out.println("debug>content-length="+len + ";");
byte[] data = Util.readFromHTTPConnection(c);
respBody=new String(data);
System.out.println("tip>respBody:" + respBody);
} catch (Exception e) {
respBody = "";//防止上级调用出错,清空
System.out.println("tip>urlthread error:" + e);
} finally {
if (c != null){
try {
c.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if (rc != HttpConnection.HTTP_OK) {
try {
throw new OAuthServiceProviderException("tip>HTTP STATU: " + rc, rc, respBody);
} catch (OAuthServiceProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getRespBody(){
return this.respBody;
}
private String url = "";
private long minPrime = 0;
private String respBody = "";
}
如果没有新开线程,这个程序就会假死,导致手机也进行不了其它操作;
debug 输出:
[0.0] debug>UTIL open url:http://api.iximo.com/request_token.php?oauth_consumer_key=7cc1a6d46dee26167629dc[0.0] d5aea5f7a804d6f33d2&oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_signature=FL95Ky3H0O74O[0.0] 5keWQ%2FMgegJKAA%3D&oauth_timestamp=1313059949&oauth_no[0.0] nce=8899455338014736064;
[0.0] tip>no in phone;
[0.0] tip>contect by wifi
[0.0] [ NSH ] OPEN 4
[0.0] TAC:TAI.handleRegRsp : ALREADY_REGISTERED
[0.0] TAC:TAI.handleRegRsp : (ALREADY_REGISTERED), pending entry copying
[0.0] debug>UTIL -- connection open;
[0.0] debug>UTIL -- got response code 200;
[0.0] debug>content-length=1010;
[0.0] tip>respBody:Invalid signature
[0.0] <hr />
[0.0] OAuthRequest Object
[0.0] (
[0.0] [parameters:protected] => Array
[0.0] (
[0.0] [oauth_consumer_key] => 7cc1a6d46dee26167629dcd5aea5f7a804d6f33d2
[0.0] [oauth_version] => 1.0
[0.0] [oauth_signature_method] => HMAC-SHA1
[0.0] [oauth_signature] => FL95Ky3H0O74O5keWQ/MgegJKAA=
[0.0] [oauth_timestamp] => 1313059949
[0.0] [oauth_nonce] => 8899455338014736064
[0.0] )
[0.0]
[0.0] [http_method:protected] => GET
[0.0] [http_url:protected] => http://api.iximo.com:80/request_token.php?oauth_consumer_key=7cc1a6d46de[0.0] e26167629dcd5aea5f7a804d6f33d2&oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_signature=FL[0.0] 95Ky3H0O74O5keWQ%2FMgegJKAA%3D&oauth_timestamp=13130599[0.0] 49&oauth_nonce=8899455338014736064
[0.0] [base_string] => GET&http%3A%2F%2Fapi.iximo.com%2Frequest_token.php&oauth_consumer_key%3D7cc1a6d[0.0] 46dee26167629dcd5aea5f7a804d6f33d2%26oauth_nonce%3D8899455338014736064%26oauth_signature_method%3DHM[0.0] AC-SHA1%26oauth_timestamp%3D1313059949%26oauth_version%[0.0] 3D1.0
[0.0] )
[0.0]