如何在主文件中创建一个新线程并启动SrcGrabber.grabSource()的启动?我应该使用什么类型的线程?没有其他线程,此代码不起作用 在主文件中,我只需要执行SrcGrabber.grabSource()并继续返回mBuffer.toString()。
public class SrcGrabber {
private HttpGet mRequest;
private HttpClient mClient;
private BufferedReader mReader;
private DocumentBuilder mBuilder;
private StringBuffer mBuffer;
private String mNewLine;
public SrcGrabber() {
mRequest = new HttpGet();
mClient = new DefaultHttpClient();
mReader = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setIgnoringComments(true);
mBuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
mBuffer = new StringBuffer(2000);
mNewLine = System.getProperty("line.separator");
}
public String grabSource(String url) throws ClientProtocolException,
IOException, URISyntaxException {
mBuffer.setLength(0);
try {
mRequest.setURI(new URI("http://www.bash.im"));
HttpResponse response = mClient.execute(mRequest);
mReader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
String line = "";
while ((line = mReader.readLine()) != null) {
mBuffer.append(line);
mBuffer.append(mNewLine);
}
} finally {
closeReader();
}
return mBuffer.toString();
}
private void closeReader() {
if (mReader == null)
return;
try {
mReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}