簡體   English   中英

將txt文件讀入變量時,獲取文本文件的url而不是java中的內容

[英]Getting the text file's url instead of the content in java when reading txt file into a variable

我是Java的新手,我試圖將Web上的文本文件讀取為變量,但是我獲取的是文本文件的url,而不是內容,並且無法弄清楚可能是什么問題。

我試圖讀取文件的類:

public class readtextfile extends AsyncTask<String, Integer, String>{

private TextView description;
public readtextfile(TextView descriptiontext){
    this.description = descriptiontext;
    }

@Override
protected String doInBackground(String... params) {
    URL url = null;
    String result ="";
    try {

        url = new URL("http://example.com/description1.txt");       
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
        result+=line;
        }
        in.close();

        } 
        catch (MalformedURLException e) {e.printStackTrace();} 
        catch (IOException e) {e.printStackTrace();}
    return result;
}

 protected void onProgressUpdate() {
    //called when the background task makes any progress
 }

  protected void onPreExecute() {
     //called before doInBackground() is started
 }

@Override
 protected void onPostExecute(String result) {
    this.description.setText(result); 
 }
  }

我稱之為課程的活動:

public class PhotosActivity extends Activity {

    TextView description;
    String descriptiontext;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photos_layout);

        description = ((TextView)findViewById(R.id.description1));

        new readtextfile(description).execute();
        }   

    }

嘗試像這樣使用Scanner

URL url = new URL("http://www.example.com/description1.txt");       
Scanner s = new Scanner(url.openStream());

嘗試使用url.openConnection和use連接對象來獲取inputStream。 更新后的方法就像

protected String doInBackground(String... params) {
    URL url = null;
    String result = "";
    try {
        url = new URL("http://www.example.com/description1.txt");
        URLConnection connection = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        in.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

根據您的評論進行更新。

您無需調用postExecute方法。 如果調用postExecute,則只需執行該方法。 doInBackground不會被執行。 相反,您應該使用execute方法。 就像java thread.start()方法調用覆蓋的run()方法一樣。

當執行異步任務時,該任務將經歷4個步驟:

onPreExecute() ,在執行任務之前在UI線程上調用。 此步驟通常用於設置任務,例如,通過在用戶界面中顯示進度欄。

doInBackground(Params ...) ,在onPreExecute()完成執行后立即在后台線程上調用。 此步驟用於執行可能需要很長時間的后台計算。 異步任務的參數將傳遞到此步驟。 計算結果必須通過此步驟返回,並將傳遞回最后一步。 此步驟還可以使用publishProgress(Progress ...)發布一個或多個進度單位。 這些值在onProgressUpdate(Progress ...)步驟中發布在UI線程上。

onProgressUpdate(Progress ...) ,在調用publishProgress(Progress ...)之后在UI線程上調用。 執行的時間是不確定的。 此方法用於在后台計算仍在執行時在用戶界面中顯示任何形式的進度。 例如,它可用於為進度欄設置動畫或在文本字段中顯示日志。

onPostExecute(Result) ,在后台計算完成后在UI線程上調用。 后台計算的結果作為參數傳遞到此步驟。

參考開發者文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM