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