测试正确的:
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;
try {
System.out.println("debug>UTIL open url:" + url + ";");
c= (HttpConnection) Connector.open(url + ";deviceside=false;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);
} 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 HttpConnection c = null;
private String respBody = "";
}
----------
把
private static class getViaHttpsConnectionThread extends Thread {
去掉static改成
private class getViaHttpsConnectionThread extends Thread {
此句
new getViaHttpsConnectionThread(143, url);
提示出错:
No enclosing instance of type Util is accessible. Must qualify the allocation with an enclosing instance of type Util (e.g. x.new A() where x is an
instance of Util).