0

Added alot of tv.setText( ) in order to know which of the lines are being executed and where does the try block breaks. The ending result of 'tv' is 4.

public class Main extends Activity {

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

    TextView tv = (TextView) findViewById(R.id.textView);

    BufferedReader reader = null;

    try{
        tv.setText("1");
        URL url = new URL("https://www.google.com");
        tv.setText("2");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        tv.setText("3");

        StringBuilder sb = new StringBuilder();
        tv.setText("4");
        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        tv.setText("5");

        String line;
        tv.setText("6");
        while((line = reader.readLine()) != null){
            tv.append(line + "\n");
        }
    }catch (Exception e){
        //tv.setText("Exception");
    }
}

I am using Android Studio and LogCat is :

04-22 23:38:25.990  28870-28870/com.example.taha.url E/Trace﹕ error opening trace file: No such file or directory (2)
04-22 23:38:26.118  28870-28870/com.example.taha.url E/linker﹕ load_library(linker.cpp:759): library "libmaliinstr.so" not found
04-22 23:38:26.125  28870-28870/com.example.taha.url E/﹕ appName=com.example.taha.url, acAppName=com.android.cts.openglperf
04-22 23:38:26.125  28870-28870/com.example.taha.url E/﹕ 0
04-22 23:38:26.125  28870-28870/com.example.taha.url E/﹕ appName=com.example.taha.url, acAppName=com.android.browser
04-22 23:38:26.125  28870-28870/com.example.taha.url E/﹕ 0
Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
Taha
  • 13
  • 5
  • It's a bit unclear that your stack trace actually reflects the crash you describe, or given the mention of com.android.browser **if it is even from your app vs some other one**. In particular, there's little obvious reason why a library would be being loaded between where you set the text to 4 and where you would next set it to 5. Show us the *full* stack trace, and consider using Log statements instead of visual display. – Chris Stratton Apr 25 '15 at 00:45
  • Also, put an e.printStackTrace() in your catch block - the empty one you have right now means you are asking the system to conceal the causes of errors from you. – Chris Stratton Apr 25 '15 at 00:52

1 Answers1

1

You Can't perform network operation on main thread you can use AsyncTask and put your network operation inside doInBackground method of AsyncTask class .

check this out : http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask

Soroush
  • 116
  • 7