0

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

问题是,当我使用有效的实际 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;

    }
}

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

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

谢谢,韦恩

4

1 回答 1

0

事实证明,即使站点重定向到没有“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;
    }
}

以下是在上一个活动中添加意图附加内容的方式:

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);
于 2016-05-03T19:24:05.287 回答