I'm running a POST request to an API during a login within my Android app to get the user's information and verify their account exists. There's a NetworkOnMainThreadException
that's coming my way when I try to do this.
logger.log(Level.INFO, "POST request URL: "+this.getUrl());
URL url = new URL(this.getUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
logger.log(Level.INFO, "Connection open");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setUseCaches(false);
OutputStream outputStream = connection.getOutputStream();
DataOutputStream wr = new DataOutputStream(outputStream); //ERROR HAPPENS HERE
The exception actually has a null error message. With research on this exception I've seen that it appears to be a threading issue where I'm running the UI on the same thread as this POST request. Is this true?
I've done this same method on a web-based application running on Glassfish and it worked fine.