![](/img/trans.png)
[英]GPS location after permission not working (SDK 23) android studio
[英]MalformedURLException in Android Studio SDK 23, site redirects to no prefix
我正在構建一個與內部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;
}
}
我的問題是,我該如何使用這樣的URL但不使用“ HTTP://”,或者如何更改站點或DNS以使其可以使用“ HTTP://”,或者我的問題出在哪里?碼?
如果有可能,我想提出一些可能性。
謝謝,韋恩
事實證明,即使站點重定向到不帶“ 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.