0

Possible Duplicate:
Strange NetworkOnMainThreadException in Android app?
Trying To Upload To Dropbox: NetworkOnMainThreadException?

I have used the below code for reading HTML contents from a url. This works perfectly for 2.3.3 but when I try to run the same code it doesn't work for ICS.

I am trying to append these html contents on to a edittext. But it always remains empty when I run the code on ICS. What may be the problem?

public class Quiz1Activity extends Activity {
    private static BufferedReader reader = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

EditText ed = (EditText) findViewById(R.id.editText1);

        try {
            ed.append(getStringFromUrl("http://www.google.com"));
            //getInputStreamFromUrl("").close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static InputStream getInputStreamFromUrl(String url){
           InputStream contentStream = null;

           try{
             HttpClient httpclient = new DefaultHttpClient();
             HttpResponse response = httpclient.execute(new HttpGet(url));
             contentStream = response.getEntity().getContent();
           } catch(Exception e){
              e.printStackTrace();
           }
           System.out.println("Content stream is " + contentStream);
           return contentStream;
        }

    public static String getStringFromUrl(String url) throws IOException{
        reader = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));

        StringBuilder sb = new StringBuilder();
        try{
        String line = null;
        while((line = reader.readLine()) != null)
        {
            sb.append(line);
        }    
        }catch (IOException e){
         e.printStackTrace();
        }
       getInputStreamFromUrl(url).close();
        return sb.toString();
    }
}
Community
  • 1
  • 1
Vinay
  • 6,891
  • 4
  • 32
  • 50

2 Answers2

2

Like @Vipul Shah said you have to move getInputStreamFromUrl() into another thread use Async Task, this is work on ICS:

package com.home.anas;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.EditText;

public class WebPageContentActivity extends Activity {
    private EditText ed;


/** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ed = (EditText) findViewById(R.id.editText1);
        readWebpage();
    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

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

    public void readWebpage() {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.google.com" });

    }
} 
K_Anas
  • 31,226
  • 9
  • 68
  • 81
0

But it always remains empty when I run the code on ICS. What may be the problem?

Issue is ICS don't allow you do asynchronous task on main thread so move your asynchronous into new thread.

You should move following code in seperate thread.

public static InputStream getInputStreamFromUrl(String url){
           InputStream contentStream = null;

           try{
             HttpClient httpclient = new DefaultHttpClient();
             HttpResponse response = httpclient.execute(new HttpGet(url));
             contentStream = response.getEntity().getContent();
           } catch(Exception e){
              e.printStackTrace();
           }
           System.out.println("Content stream is " + contentStream);
           return contentStream;
        }
Vipul
  • 27,808
  • 7
  • 60
  • 75