我有一个要求,我必须获取在编辑文本中输入的 URL 地址的来源。能够获取在 HTTPGET 请求中硬编码的 URL 的源文件。代码如下..
public String getHtml() throws ClientProtocolException, IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.google.com");
HttpResponse response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
Log.d("result","="+result);
}
return result;
}
并将其保存在文件中,如:
public void onClick(View v) {
File file = new File(etPath.getText().toString());
FileWriter writer=null;
try
{
writer = new FileWriter(file);
/** Saving the contents to the file*/
writer.write(getHtml());
/** Closing the writer object */
writer.close();
/** Getting sharedpreferences object to store the path of last saved file */
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
/** Setting the last saved file's path in the shared preference */
editor.putString("fpath", file.getPath());
/** Save the path to the shared preference */
editor.commit();
Toast.makeText(getBaseContext(), "Successfully saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
};
此方法正确返回 google.com 的来源。但我需要做这样的事情:
HttpGet httpGet = new HttpGet(etContent.getText().toString());
其中 etContent 是输入 URL 的编辑文本。但是当我执行这个语句时,它崩溃了。
我需要提取在编辑文本中输入的 URL 的来源。我不打算使用异步任务。请帮帮我。
请发布一些代码以支持。谢谢