2

I would like to get content of an Internet txt file. When I open it with emulator I get this message

"Unfortunately application has stopped".

Nothing happens if remove "http://" but it doesnt read anything and becomes null. What's wrong? How can read the url above? I have added permission.

This is the whole code i changed but still get same error. what mistake i do? http://dforum.zxq.net/code.txt

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... arg0) {



            try {
                URL url = new URL("http://dforum.zxq.net/myfile.txt");

                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                String str;
                while ((str = in.readLine()) != null) {
                    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();      
                    // str is one line of text; readLine() strips the newline character(s)
                }
                in.close();
            } catch (MalformedURLException e) {
            } catch (IOException e) {
            }




            return null;
            // TODO Auto-generated method stub

        }


     }.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

2 Answers2

1

Make sure that you don't do that on the UI thread and also check that you have the internet permission.

Both cases can cause an exception which let your app crash.

Here is an example about how to prevent doing network operations on the GUI thread:

new AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... params) {

        // add your try catch block here.

        return null;
    }
}.execute();
rekire
  • 47,260
  • 30
  • 167
  • 264
0

Make sure you taking this process off the main UI thread by running it inside an AsyncTask. Check out this tutorial: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/. More specifically, check out the JSONParser class in there.

UPDATE

You are calling Toast in a while loop, which I think is the reason it's crashing. Replace it with Log.d() or System.out.println()

Johnathan Au
  • 5,244
  • 18
  • 70
  • 128
  • i did all you say also add this i wrote whole code above? so why its closed again? – yakamoz 2005 Apr 26 '13 at 22:18
  • Update your answer with the log output if it's different. – Johnathan Au Apr 26 '13 at 22:49
  • try creating an inner class for AsyncTask instead like in the tutorials I linked above. – Johnathan Au Apr 26 '13 at 22:49
  • I think it's because your calling Toast in a while loop, thus making it crash. Remove the while loop and then use System.out.println() instead – Johnathan Au Apr 26 '13 at 22:51
  • i changed toast as System.out.println(str); But it doesn't read and write anything. it seems null? i would be very happy if you correct it and share it as project in zip file hear. i think this is best solution coz i tried all i can do. thanks everyone who helps me. – yakamoz 2005 Apr 26 '13 at 23:26
  • This is the log. url is exist. 04-26 23:35:55.066: E/Trace(1098): error opening trace file: No such file or directory – yakamoz 2005 Apr 26 '13 at 23:38
  • Try something like this: http://stackoverflow.com/questions/6099429/how-to-read-text-file-in-android-from-web – Johnathan Au Apr 26 '13 at 23:43
  • before you do that. try this: new URL(url).openConnection(); i.e. you're not opening the connection in your code – Johnathan Au Apr 26 '13 at 23:44