MalformedURLException in Android Studio SDK 23, site redirects to no prefix

130 Views Asked by At

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

1

There are 1 best solutions below

2
On

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.

public class DoLogin extends AsyncTask<Void, Void, String> {
    protected void onPostExecute(String result) {
        PostLoginStatus(result);
    }
    protected String doInBackground(Void... params) {
        String username = "No Username passed.";
        String password = "No Password passed.";
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            username = extras.getString("Username");
            password = extras.getString("Password");
        }
        String charset = "UTF-8";
        String pageText = "";
        try {
            URL url = new URL("http://MyServer.MyOrg.k12.mo.us/service/login.asp?ucode=" + username + "&upassword=" + password);
            URLConnection connection = url.openConnection();
            // wrap the urlconnection in a bufferedreader
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;

            // read from the urlconnection via the bufferedreader
            while ((line = bufferedReader.readLine()) != null)
            {
                pageText = pageText + line + "\n";
            }
            bufferedReader.close();

        } catch (MalformedURLException e) {
           pageText = "Username: " + username + " Password: " + password + "MalformedURLException " + e.getMessage();
        } catch (IOException e2) {
           pageText = "Username: " + username + " Password: " + password + " IOException " + e2.getMessage();
        } catch (NullPointerException e3) {
           pageText = "Username: " + username + " Password: " + password + " NullPointerException " + e3.getMessage();
        }
        return pageText;
    }
}

Here's how the intent extras were added in the previous activity:

String username = eusername.getText().toString().trim();
String password = epassword.getText().toString().trim();
Intent i = new Intent(Login.this, VerifyInventory.class);
// Where 'login' would be the current activity's name and 'VerifyInventory' is the next activity's name.
i.putExtra("Username", username);
i.putExtra("Password", password);
startActivity(i);