VovaG
VovaG

Reputation: 51

Android Studio return exeption from GitHub API request

I am creating Android app to get repository info from GitHub account. Function to get repositories data from GitHub account https://github.com/vGrynishyn. I received result only 2-3 times using Android Studio , after that I receive the following Exception in urlConnection.getResponseCode(): android.os.NetworkOnMainThreadException. But I tried run it in Intelij IDEA(Command line app) and it is working all time without problem.

Could you please advise me where is error?

private String getGitHubRepositoryContent(){
            String line = null;
            try {
                URL url = new URL("https://api.github.com/users/vGrynishyn/repos");

                HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                int responseCode = urlConnection.getResponseCode();

                InputStream in;
                if (urlConnection.getResponseCode() < HttpsURLConnection.HTTP_BAD_REQUEST) {
                    in = urlConnection.getInputStream();
                } else {
                    in = urlConnection.getErrorStream();
                }

                BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                while (br.read() != -1) {
                    line = br.readLine();
                }

                System.out.println("Response code: " + responseCode);
                System.out.println(line);

            } catch (Exception e) {
                e.printStackTrace(); //do something
            }
            return line;
    }

debug window screen

Upvotes: 2

Views: 95

Answers (2)

capt.swag
capt.swag

Reputation: 10661

The problem here is you're running your method getGitHubRepositoryContent() on the main thread. This is not the right way to do network requests on Android. I would also recommend against using HttpsURLConnection. Use Retrofit or Volley for making HTTP requests since they themselves take care of making the network call in a background thread, and gives callback of the HTTP response in the main thread.

But I tried run it in Intelij IDEA(Command line app) and it is working all time without problem.

This runs without error in command line because there are no separate UI events to be handled. In command line when a request is made, your code makes the request on same thread it's running on, which freezes the program, but it isn't noticeable because there is no separate UI to see a noticeable freeze.

TL;DR You shouldn't make network requests on the main thread in Android. It's recommended to use Retrofit or Volley.

Upvotes: 1

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8713

The error suggests you are executing a network operation in the main thread. Android OS doesn't allow you to do It in order to prevent your UI to freeze. Perform network operation in another thread, use Retrofit library or Asyntasks to make network operations

Upvotes: 1

Related Questions