【问题标题】:MalformedURLException in Android Studio SDK 23, site redirects to no prefixAndroid Studio SDK 23 中的 MalformedURLException,站点重定向到无前缀
【发布时间】:2016-05-03 16:32:59
【问题描述】:

我正在构建一个与我工作中的内部网络服务器通信的应用程序,它有一个登录过程。

问题是,当我使用有效的实际 URL 时,我得到了 MalformedURLException。当我在我的应用程序中使用带有地址的 http:// 时,我收到一个错误:

com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream@42c06aa0

如果我在 Firefox 或 Chrome 等浏览器的地址上添加 http://,该站点将重定向到自身,但没有 http://。我认为它以某种方式设计为不使用“http://”部分,但我不确定如何解决这个问题。

我已经搜索了几个小时关于使用 HttpUrlConnection 的信息,我发现的只是 URL 需要该协议。

这是我的代码现在的样子,出于隐私原因,我已将实际 URL 替换为“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;

    }
}

我的问题是,我怎样才能使用这样的 URL 但没有“HTTP://”,或者如何更改站点或 DNS 以便它可以使用“HTTP://”,或者有什么我的代码有问题吗?

如果可能的话,我想提供一些可能性。

谢谢, 韦恩

【问题讨论】:

  • 在 chrome 中打开该页面时,服务器是否会将您重定向到另一个页面?
  • I think it's somehow designed to not operate with the "http://" part 它被称为“协议”,并且总是有一个指定的。在您的情况下,http 或者,因为您很可能更喜欢通过安全连接发送密码,https。你试过吗?
  • “我得到一个错误”——这不是一个错误。那是在 com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream 对象上调用的 toString() 的输出。如果您正在崩溃,您可能希望在您的问题中发布完整的堆栈跟踪。
  • @Basil-Battikhi 是的,当我在 Chrome 中尝试在 URL 前加上 http:// 时,“http://”部分实际上已从 URL 中删除,然后是其余部分URL 保持不变,并显示我想要的页面。

标签: android malformedurlexception


【解决方案1】:

事实证明,即使网站重定向到没有“http://”部分的 URL,这也无关紧要。

实际的问题在于我是如何尝试读取由 Web 服务器提供的页面,并且我没有正确传递我的 intent.extra 值!

这是工作代码,它现在有调试的东西,但希望其他人能得到它的帮助。

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;
    }
}

以下是在上一个活动中添加 Intent Extras 的方式:

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);

【讨论】:

  • 你处理了重定向响应吗?怎么样?
  • 我根本没有处理它。上面的代码读取 Web 服务器将您重定向到的页面。现在我对饼干感到忧郁......这将是一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
  • 2015-08-06
相关资源
最近更新 更多