0

我有一个要求,我必须获取在编辑文本中输入的 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 的来源。我不打算使用异步任务。请帮帮我。

请发布一些代码以支持。谢谢

4

1 回答 1

0

自己解决了问题。问题是我在输入 URL 时没有附加 http://。它现在工作正常。谢谢大家:-)

于 2012-12-11T07:29:56.117 回答