我正在构建一个与我工作中的内部 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://”,或者我的有什么问题吗?代码?
如果可能的话,我想提出一些可能性。
谢谢,韦恩