3

I am totally, desperate, that is why I am asking you for help. I have the following code in an android application:

public void OnButtonClick(View view) throws ClientProtocolException, IOException, Exception
{       
    URL yahoo = new URL("http://privyrobsi.sk/brigades/getBrigades.json");
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                            yc.getInputStream()));
    String inputLine;

    EditText ET = (EditText) findViewById(R.id.editText2);
    while ((inputLine = in.readLine()) != null) ET.setText(ET.getText()+inputLine);         
    in.close();        
}

I got this code from here, I only modified it a little. When I try to run the code as a stand-alone java application, it works. But when I try to use it in android, the emulator says that the unfortunately, the application stopped. I saved the Log from LogCat here. I also have uses-permission android:name="android.permission.INTERNET" in my manifest file.

Community
  • 1
  • 1

1 Answers1

4

you can not perform network operation in main thread. Perform this in a asynctask.

Update:

Your logcat data shows you have a NetworOnMainThreadException. According to doc

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

Solution:

As I have already mentioned you have to use a different thread to perform Network operations. You can use the following

  1. Asynctask
  2. Differet worker thread
  3. Handler

Check this thread. The answers of this question cover almost all the solutions

Community
  • 1
  • 1
stinepike
  • 50,967
  • 14
  • 89
  • 108