2017-05-03 49 views
0

我在這裏問的所有其他解決方案沒有奏效。我想從網上讀取文本文件,並將此字符串放入文本視圖中。我現在只是在測試,文本文件中唯一的值是「223」。我的應用開始崩潰任何人都可以請幫忙?從URL讀取文本文件並輸出到TextView

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.StringBuilderPrinter; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

public class MainActivity extends AppCompatActivity { 

    private TextView textView; 
    private StringBuilder text = new StringBuilder(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     BufferedReader reader = null; 

     try { 

      URL url = new URL("http://something.uk/pmt/status.txt"); 


      reader = new BufferedReader(
        new InputStreamReader(url.openStream())); 


      String str; 


      while ((str = reader.readLine()) != null) { 
       text.append(str); 
       text.append('\n'); 
      } 

     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), "Error reading file.", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } finally { 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (MalformedURLException e) { 

       } 

       catch (IOException e) { 

       } 
      } 

      TextView output = (TextView) findViewById(R.id.textView); 
      output.setText((CharSequence) text); 
     } 
    } 
} 

堆棧跟蹤:

https://pastebin.com/YSCB9RBg

+1

請從logcat發佈您的錯誤 –

+0

如何使用logcat?我在終端上試過adb logcat,但沒有奏效。 – JB2000

+0

錯誤很明顯...... NetworkOnMainThread ....這意味着你必須從另一個線程的try catch執行代碼。 –

回答

1

可能是因爲您使用StringBuilder這不是CharSequence。使用output.setText(text.toString());代替output.setText((CharSequence) text);

TextView.setText()預計CharSequence作爲參數。 StringCharSequence,但StringBuilder不是。要得到一個String你給你打電話StringBuilder.toString()

你應該看看崩潰。並且在下一次你在stackoverflow上提問時發佈它。

您提供的崩潰日誌清楚地說明了崩潰的原因:android.os.NetworkOnMainThreadException。這意味着您正在嘗試在主線程上執行網絡操作,並且Android操作系統不允許您。自Honeycomb以來,這是一條規則。例如,解決方案是使用AsyncTask。這裏是an article about network ops and AsyncTask

+0

你是什麼意思由stringbuilder不是一個charsequence? – Remario

+0

@Remario擴展了答案。 –

+0

感謝您的回答,我已經完成了您的建議,但應用程序仍然崩潰。 – JB2000

1
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.StringBuilderPrinter; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

public class MainActivity extends AppCompatActivity { 

    private TextView textView; 
    private StringBuilder text = new StringBuilder(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     BufferedReader reader = null; 

     new AsyncTask<Void,Void,Void>(){ 


      @Override 
      protected Void doInBackground(Void... params) { 
try { 

      URL url = new URL("http://something.uk/pmt/status.txt"); 


      reader = new BufferedReader(
        new InputStreamReader(url.openStream())); 


      String str; 


      while ((str = reader.readLine()) != null) { 
       text.append(str); 
       text.append('\n'); 
      } 

     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), "Error reading file.", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } finally { 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (MalformedURLException e) { 

       } 

       catch (IOException e) { 

       } 
      } 
       return null; 
      } 

    @Override 
      protected void onPostExecute(Void aVoid) { 
       super.onPostExecute(aVoid); 
      TextView output = (TextView) findViewById(R.id.textView); 
      output.setText(text.toString()); 
      } 
     }.execute(null,null,null); 


     } 
    } 
} 
相關問題