I'm building an app that communicates with a internal web server at my work, it has a login process.
The problem is, when I use the actual URL that works, I get a MalformedURLException. And when I use http:// with the address in my app, I get an error:
com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream@42c06aa0
If I add http:// onto the address in a browser like Firefox or Chrome, the site redirects to itself but without the http://. I think it's somehow designed to not operate with the "http://" part but I'm not sure how to work around this.
I've searched for a few hours now about using the HttpUrlConnection and all I've found is that the protocol is required with URLs.
Here's what my code looks like right now, I've replaced the actual URL for privacy reasons with "MyServerName","MyOrgName".
public class DoLogin extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Bundle extras = getIntent().getExtras();
String username = extras.getString("Username");
String password = extras.getString("Password");
String charset = "UTF-8";
String pageText = "";
try {
URL url = new URL("http://MyServerName.MyOrgName.k12.mo.us/service/login.asp?ucode=" + username + "&upassword=" + password);
//URL url = new URL("http://www.google.com/");
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
pageText = response.toString();
} catch (MalformedURLException e) {
//Do something with the exception.
pageText = "MalformedURLException " + e.getMessage();
} catch (IOException e2) {
//Do something with the exception.
pageText = "IOException " + e2.getMessage();
} catch (NullPointerException e3) {
pageText = "NullPointerException " + e3.getMessage();
}
setData(pageText);
return null;
}
}
My question is, how can I either use a URL like this but without "HTTP://", or how can I change the site or DNS so that it can use "HTTP://", or is there something wrong with my code?
If at all possible, I'd like to have a few possibilities presented.
Thanks, Wayne
Turns out that even though the site redirects to a URL without the "http://" part, that this doesn't matter.
The actual problem was in how I was trying to read the page served by the web server AND that I was not passing my intent.extra values correctly!
Here is working code, It's got debugging stuff in it right now but hopefully someone else is helped by it.
Here's how the intent extras were added in the previous activity: