-2

Trying to read a webpage as an String this is my code :

    public class ReadWebPage extends Activity {
private EditText url_text;
private TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    url_text = (EditText) findViewById(R.id.address);
    textView = (TextView) findViewById(R.id.tv);
}
public void myButtonClickHandler(View view) {
    switch (view.getId()) {
    case R.id.ReadWebPage:
        try {
            if (!url_text.getText().toString().trim().equalsIgnoreCase("")) {
                textView.setText("");
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet(url_text.getText().toString());
                // Get the response
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String response_str = client.execute(request,
                        responseHandler);
                textView.setText(response_str);
            } else {
                Toast.makeText(getApplicationContext(),
                        "URL String empty.", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            System.out.println("Some error occured.");
            textView.setText(e.getMessage());
        }
        break;
    }
}
    }

As above i show my code using that code and trying to read some webpage as String but it is showing that error.

  03-03 22:37:45.088: I/System.out(1233): Some error occured.
   03-03 22:37:45.088: W/System.err(1233): android.os.NetworkOnMainThreadException
  03-03 22:37:45.138: W/System.err(1233):   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)    org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
 -03 22:37:45.148: W/System.err(1233):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  03-03 22:37:45.158: W/System.err(1233):   at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)

1 Answers1

0

Android is telling you that you shouldn't be doing network operations on the UI thread. It doesn't want you to block the thread, and for good reason.

Try using an AsyncTask instead. Put your HTTP request code in the doInBackground() method and update the TextView in the onPostExecute() method. You CANNOT access the UI thread from the doInBackground() method.

Here's a really basic example:

class MyAsyncTask extends AsyncTask<String, String, String> 
{
    private TextView textView;

    public MyAsyncTask(TextView textView)
    {
        this.textView = textView;
    }

    protected Long doInBackground(String... params) 
    {
        String url = params[0];
        // your HTTP request code goes here
        return response;
    }

    protected void onPostExecute(String result) 
    {
        textView.setText(result);
    }
}

Use the class like so:

new MyAsyncTask(myTextView).execute(myUrl);
Tyler MacDonell
  • 2,609
  • 18
  • 27