0

I'm actually srugling with my application using HttpUrlConnection, I tried the simplest code found on the internet and still got a FatalShutDown in the logcat and I don't understand the problem.

Here is the code :

try {
     URL url = new URL("http://www.android.com/");
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     BufferedReader r = new BufferedReader(new InputStreamReader(in));
     StringBuilder total = new StringBuilder();
     String line;
     while ((line = r.readLine()) != null) {
           total.append(line).append('\n');
     }
     response_textview.setText(total.toString());
     urlConnection.disconnect();
 }
 catch (MalformedURLException e){
      response_textview.setText(e.getMessage());
 }
 catch (IOException e){
      response_textview.setText(e.getMessage());
 }

And I got this in the logcat :

Logcat error

Thanks

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Altane
  • 11
  • 2
    Possible duplicate of [How to fix android.os.NetworkOnMainThreadException?](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) – OneCricketeer May 21 '17 at 16:33
  • This "simplest code" is meant to be ran in pure Java, not as an Android app. Please refer to the Android networking documentation – OneCricketeer May 21 '17 at 16:35

3 Answers3

1

You need to make request in new thread. Probably, your code (it is only try catch) is running on main thread. This operation is denied. Try to use e.g. Thread.class to make request in new thread.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Master Disaster
  • 729
  • 4
  • 21
  • How can set text view value in a thread? response_textview.setText(e.getMessage()); You can't update UI in the background. – PEHLAJ May 21 '17 at 17:18
  • It is only example but still you can use runOnUIThread. It is bad way exactly same as pass text view to async task without wrapping TextView with WeakReference. Ideal way is to use RxJava. – Master Disaster May 21 '17 at 17:38
  • No need to pass text view to Async task. Async task object is created in the same class and can access text view directly. – PEHLAJ May 21 '17 at 17:40
  • Just see into you `doInBackground` method. How do you want to update `response_textview` in `catch`? – Master Disaster May 21 '17 at 17:47
  • Good catch, that's copy paste issue. I copied actual question and put it into doInBackground. Anyway it is updated now. Thanks – PEHLAJ May 21 '17 at 17:49
1

Wrap your code with Asynktask doInBackground() method.

krsnk
  • 267
  • 1
  • 10
1

You shouldn't perform network operation on main thread. Use AsyncTask.

1. Create AsyncTask object and move your code into doInBackground method as mentioned below:

AsyncTask task = new AsyncTask<Void,String,String>(){

    @Override
    protected String doInBackground(Void... params) {
    // perform your network operation here and return response

       try {
           URL url = new URL("http://www.android.com/");
           HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
           InputStream in = new BufferedInputStream(urlConnection.getInputStream());
           BufferedReader r = new BufferedReader(new InputStreamReader(in));
           StringBuilder total = new StringBuilder();
           String line;
           while ((line = r.readLine()) != null) {
                total.append(line).append('\n');
           }

           urlConnection.disconnect();
       }
       catch (MalformedURLException e){
            return e.getMessage();
       }
       catch (IOException e){
            return e.getMessage();
       }
       return total.toString();

    }

    protected void onPostExecute(String response){
             // response returned by doInBackGround() will be received 
             // by onPostExecute(String response)
              response_textview.setText(response);
    }

  };

2. Execute task

 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53