0

I am totally new to android and trying to develop a basic application. I have used the default Navigation Drawer Activity from android while creating the project.

Basically, I am getting an exception when I am making an HTTP get req upon a button click. I have created a button that is all working fine. The button is created inside the HomeFragment.java. Here is the code -

   final Button button = root.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
           // new ApiCall("637", "22-05-2021", textView).start();
            try {
               // String finalUrl = address+"district_id="+this.district_id+"&date="+this.date;
                URL url = new URL("https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict/?district_id=637&date=22-05-2021");
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                con.setRequestProperty("User-Agent", "PostmanRuntime/7.26.10");
                con.setRequestProperty("Accept-Language", "en_US");
                con.setRequestProperty("Content-Type", "application/json");
                con.setRequestProperty("Accept", "application/json");

               //Exception
                BufferedReader in = new BufferedReader(new InputStreamReader( con.getInputStream())); // here exception coming////////////////////

                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                }
                in.close();
            } catch(MalformedURLException e) {
                System.out.println("Malformed URL: " + e.getMessage());
                textView.setText("Malformed");

            } catch(IOException e) {
                System.out.println("IOException: " + e.getMessage());
                textView.setText("Exception");

            }
            catch (Exception e){
                textView.setText("Exception");
                e.printStackTrace();
            }


        }
    });

This code of calling HTTP get req is all working fine when I do it purely in java in IntelliJ Idea. I don't know why am I getting exceptions here. Let me know

Thanks, Akhi

ABD
  • 180
  • 11
  • 2
    I think its NetworkOnMainThread exception .. try to use executor or some other background thread to perform api calls . https://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception – Pwn May 22 '21 at 17:34

1 Answers1

1

As @Bhardwaj suggested, you can't make network calls in the main thread in Android. Create a background thread like this:

class RetrieveRemoteData extends AsyncTask<String, Void, YourResponseModelClass> {

    private Exception exception;

    protected YourResponseModelClass doInBackground(String... urls) {
        // new ApiCall("637", "22-05-2021", textView).start();
            try {
               // String finalUrl = address+"district_id="+this.district_id+"&date="+this.date;
                URL url = new URL("https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict/?district_id=637&date=22-05-2021");
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                con.setRequestProperty("User-Agent", "PostmanRuntime/7.26.10");
                con.setRequestProperty("Accept-Language", "en_US");
                con.setRequestProperty("Content-Type", "application/json");
                con.setRequestProperty("Accept", "application/json");

               //Exception
                BufferedReader in = new BufferedReader(new InputStreamReader( con.getInputStream())); // here exception coming////////////////////

                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                }
                in.close();
            } catch(MalformedURLException e) {
                System.out.println("Malformed URL: " + e.getMessage());
                textView.setText("Malformed");

            } catch(IOException e) {
                System.out.println("IOException: " + e.getMessage());
                textView.setText("Exception");

            }
            catch (Exception e){
                textView.setText("Exception");
                e.printStackTrace();
            }


        }
    }

    protected void onPostExecute(YourResponseModelClass responseObject) {
        // write your code here what happens after getting response or exception
    }
}

You can refer to this, this has a lot of detailed steps for your use case.

Additionally, try to include more details to your question like what exceptions are you getting to be more descriptive about your issues so that the contributors understand your question better and suggest you accurate solutions.

Yash Joshi
  • 557
  • 7
  • 25