0
我有一个要求,我必须获取在编辑文本中输入的URL地址的来源。能够获取在HTTPGET请求中硬编码的URL的源文件。代码如下..获取在编辑文本中输入的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的来源。我不打算使用异步任务。请帮帮我。
请张贴一些支持的代码。谢谢