Search code examples
androidstringurl

Android read Text from a website


I'm pretty new to Android programming, however I'm used to program for iOS though...

Now in iOS you have the function of NSString stringWithContentOfURL. Now my Question is, if there is a similar option in java as well...? While searching in google i've come through some examples, but using them my app always crashes...

I used codes like this: http://www.coderzheaven.com/2011/07/17/how-to-read-webpage-contents-as-a-string-in-android/ but it keeps crashing... Could you please help me?

Thank you!

Edit: LogCat Errors:

01-22 19:34:12.718: E/AndroidRuntime(5481): FATAL EXCEPTION: main
01-22 19:34:12.718: E/AndroidRuntime(5481): android.os.NetworkOnMainThreadException
01-22 19:34:12.718: E/AndroidRuntime(5481): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-22 19:34:12.718: E/AndroidRuntime(5481): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-22 19:34:12.718: E/AndroidRuntime(5481): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-22 19:34:12.718: E/AndroidRuntime(5481): at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at com.example.test4.DieActivity.meldeAn(DieActivity.java:150)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at com.example.test4.DieActivity$1.onClick(DieActivity.java:56)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.view.View.performClick(View.java:3511)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.view.View$PerformClick.run(View.java:14105)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.os.Handler.handleCallback(Handler.java:605)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.os.Looper.loop(Looper.java:137)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • you have tried to perform (network) IO on the UI thread. this is not allowed. why? because if you hang the UI thread, the system will post an ANR (activity not responding) dialog to the user..

    you must perform IO asynchronously,

    new AysyncTask<String,Void,String>() {
                protected String doInBackground(String... urls) {
                  String url = urls[0];
                  // do network operation, get result in a string (for example)
                  return resultString;
                }
    
                protected void onPostExecute(String result) {
                  JSONObject jo = new JSONObject(result);
                  // update UI with the results of the network call here
                }
            }.execute(aUrl);
    

    this is especially important for network IO, but it follows for disk IO as well. take a look at AsyncTask for details. the interface for AsyncTask is a little obtuse IMHO.

    there's nothing special about AsyncTask, it's wraps a pattern around creating a thread and updating the user interface based on the results. in the old days, we used to do it like this,

    new Thread(new Runnable() { 
      @Override
      public void run() {
        // do network operation
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            // update UI thread here
          }
        });
      }
    }}.start();