2012-12-11 42 views
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的來源。我不打算使用異步任務。請幫幫我。

請張貼一些支持的代碼。謝謝

回答

0

自己解決了poroblem。問題是我輸入URL時沒有附加http://。它的工作現在很好。謝謝大家:-)

相關問題