Marcus Thornton
Marcus Thornton

Reputation: 6193

Reading Contents from a URL leads to crash in Android App

I have problem in this Android app. When I press the button_x it always crashes. The program is listed below. I used BufferedReader to read content from the Internet.

public class MainActivity extends Activity {

    Button button_x;

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

        button_x = (Button)findViewById(R.id.button_x);

        button_x.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                URL yahoo = null;
                try {
                    yahoo = new URL("http://www.google.com.tw/");
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                BufferedReader in = null;
                try {
                    in = new BufferedReader(
                                new InputStreamReader(
                                yahoo.openStream()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                String inputLine;

                try {
                    while ((inputLine = in.readLine()) != null)
                        button_x.setBackgroundColor(Color.RED);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });


    }

    @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;
    }

}

I thought it was caused by the Internet permission, so I add the Internet permission on the manifest file.

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />"

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.euro.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Could any one tell me why this program would crash?

Upvotes: 0

Views: 258

Answers (1)

cnnr
cnnr

Reputation: 1636

You can't do networking on the UI thread in Android. You'll need to create a new Thread or use an AsyncTask. According to Connecting to the Network from the documentation:

Network operations can involve unpredictable delays. To prevent this from causing a poor user experience, always perform network operations on a separate thread from the UI. The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread.

Upvotes: 1

Related Questions