-3

I'm new to Android development and try to create a simple app which should contact a web server and get a request Api.

I write this simple java code on InteliJ IDEA and work correctly

but when write similar thing in Android Studio get an android.os.NetworkOnMainThreadException

Can anybody explain why get an exception

InteliJ IDEA code :

package sample;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main  {

    public static void main(String[] args) {

        HttpURLConnection urlConnection = null;
        try {
            URL url = new URL("http://codeforces.com/api/user.rating?handle=Adel_SaadEddin");

            urlConnection = (HttpURLConnection) url.openConnection();

            InputStream in = urlConnection.getInputStream();
            InputStreamReader isw = new InputStreamReader(in);

            StringBuilder stringBuilder = new StringBuilder();

            int data = isw.read();
            while (data != -1) {
                char current = (char) data;
                data = isw.read();

                stringBuilder.append(current);
            }

            String res = stringBuilder.toString();

            System.out.print(res);
        } catch (Exception ex) {
            System.out.print("Error");
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }
}

Android Studio :

MainActivity.java

package android.adelsaadeddin.app3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

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

        TextView textView = (TextView) findViewById(R.id.text);
        HttpURLConnection urlConnection = null;
        try {
            URL url = new URL("http://codeforces.com/api/user.rating?handle=Adel_SaadEddin");

            urlConnection = (HttpURLConnection) url.openConnection();

            InputStream in = urlConnection.getInputStream();
            InputStreamReader isw = new InputStreamReader(in);

            StringBuilder stringBuilder = new StringBuilder();

            int data = isw.read();
            while (data != -1) {
                char current = (char) data;
                data = isw.read();

                stringBuilder.append(current);
            }

            String res = stringBuilder.toString();

            textView.setText(res);
        } catch (Exception ex) {
            textView.setText("Error");
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }

    }
}

AndroidManifest.xml :

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.adelsaadeddin.app3">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="android.adelsaadeddin.app3.MainActivity">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Logcat :

01-19 15:03:32.812 4379-4379/? I/art: Not late-enabling -Xcheck:jni (already on)
01-19 15:03:32.813 4379-4379/? W/art: Unexpected CPU variant for X86 using defaults: x86
01-19 15:03:32.885 4379-4379/android.adelsaadeddin.app3 W/ActivityThread: Application android.adelsaadeddin.app3 is waiting for the debugger on port 8100...
01-19 15:03:32.887 4379-4379/android.adelsaadeddin.app3 I/System.out: Sending WAIT chunk
01-19 15:03:34.385 4379-4386/android.adelsaadeddin.app3 I/art: Debugger is active
01-19 15:03:34.513 4379-4379/android.adelsaadeddin.app3 I/System.out: Debugger has connected
01-19 15:03:34.514 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:34.715 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:34.917 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.119 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.321 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.527 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.731 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.932 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:36.133 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:36.335 4379-4379/android.adelsaadeddin.app3 I/System.out: debugger has settled (1471)
01-19 15:03:36.444 4379-4379/android.adelsaadeddin.app3 W/System: ClassLoader referenced unknown path: /data/app/android.adelsaadeddin.app3-1/lib/x86
01-19 15:03:36.561 4379-4379/android.adelsaadeddin.app3 I/InstantRun: starting instant run server: is main process
01-19 15:03:36.705 4379-4386/android.adelsaadeddin.app3 I/art: Starting a blocking GC Instrumentation
01-19 15:03:36.850 4379-4379/android.adelsaadeddin.app3 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-19 15:03:47.223 4379-4379/android.adelsaadeddin.app3 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-19 15:05:40.325 4379-4449/android.adelsaadeddin.app3 I/OpenGLRenderer: Initialized EGL, version 1.4
01-19 15:05:40.326 4379-4449/android.adelsaadeddin.app3 D/OpenGLRenderer: Swap behavior 1
01-19 15:05:40.326 4379-4449/android.adelsaadeddin.app3 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
01-19 15:05:40.326 4379-4449/android.adelsaadeddin.app3 D/OpenGLRenderer: Swap behavior 0
01-19 15:05:40.389 4379-4449/android.adelsaadeddin.app3 D/EGL_emulation: eglCreateContext: 0xa658e9e0: maj 2 min 0 rcv 2
01-19 15:05:40.717 4379-4379/android.adelsaadeddin.app3 W/art: Verification of void android.support.v7.widget.AppCompatImageView.setBackgroundResource(int) took 135.260ms
01-19 15:05:40.735 4379-4379/android.adelsaadeddin.app3 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
01-19 15:05:40.811 4379-4449/android.adelsaadeddin.app3 D/EGL_emulation: eglMakeCurrent: 0xa658e9e0: ver 2 0 (tinfo 0xa3bcd120)
01-19 15:05:40.868 4379-4449/android.adelsaadeddin.app3 D/EGL_emulation: eglMakeCurrent: 0xa658e9e0: ver 2 0 (tinfo 0xa3bcd120)
Adel SaadEddin
  • 83
  • 2
  • 10

2 Answers2

0

You can not send request in application's main thread. You should use AsyncTask or background thread to send request. (I recommend you to use Volley or Retrofit) Check these links :
How do I fix android.os.NetworkOnMainThreadException?
http://simpledeveloper.com/network-on-main-thread-error-solution/

Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26
0

You are doing long time operations in the main thread that throws an Exception

android.os.NetworkOnMainThreadException


Use Async Task

new AsyncTask<String,String,String>(){

@Override
protected String doInBackground(String... params) {
    
    //Your HTTP Request goes here
    
    return response;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    Log.i("UpdateJSON ", s);
    if(!s.equalsIgnoreCase("null") || !s.equalsIgnoreCase(""))
        writeManifestJSON(s);
}
}.execute();

OR

I solved this problem using a new Thread. Which will not work in the main thread.

Thread thread = new Thread(new Runnable() {
    
    @Override
    public void run() {
        try  {
            //Your HTTP Request goes here
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

thread.start(); 

        
Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89