Android 获取在编辑文本中输入的URL的源
我有一个要求,在那里我必须得到的URL地址是在编辑文本中输入的来源。我能够获取在HTTPGET请求中硬编码的URL的源文件。代码如下Android 获取在编辑文本中输入的URL的源,android,Android,我有一个要求,在那里我必须得到的URL地址是在编辑文本中输入的来源。我能够获取在HTTPGET请求中硬编码的URL的源文件。代码如下 public String getHtml() throws ClientProtocolException, IOException { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); H
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的源。我不打算使用异步任务。请帮帮我,伙计们
请发布一些支持代码。谢谢我自己解决了这个问题。问题是我在输入URL时没有附加http://地址。现在工作正常了。谢谢大家:-)