首先:我是编程的新手,这是一个学校项目。
我的问题:我有一个使用字符串保存用户凭据的基本登录掩码。凭据(如果为true)将通过SharedPrefences保存。现在,我想将用户输入与Web服务器http://fost19.tk/(用户:fost19,密码:12345)的基本身份验证凭据进行比较,以将用户登录到应用程序中或向其显示错误消息。到目前为止,一切正常,但是我一直无法连接到我的Web服务器并比较凭据。
我已经在互联网上阅读了一些问题,但是老实说,我现在已经完全迷失了,这是在当前android版本中实现此目标的最佳方法。我知道我需要从Web服务器解密给定的凭据,但是我不知道如何在Java中执行此操作,也不知道需要在项目中添加哪些“插件”。
感谢您的帮助! :)
答案 0 :(得分:0)
下一步,您可能要从网站下载内容并在应用程序中进行处理。 首先,您的应用程序需要Internet许可。将以下行添加到清单文件中的application标记下:
<uses-permission android:name="android.permission.INTERNET" />
您的网站需要http认证。为此,您需要在请求的标题中包含用户名和密码。 但是,Android不允许同步网络流量。因此,我们在后台从网站下载内容,然后可以通过回调更新用户界面。您可以将我的类DownloadContent添加到您的项目中:
import android.os.AsyncTask;
import android.util.Base64;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import javax.net.ssl.HttpsURLConnection;
public class DownloadContent extends AsyncTask<String, Void, String> {
private final Callback callback;
private final String username, password;
private DownloadContent(@NonNull String username, @NonNull String password, @NonNull Callback callback) {
this.callback = callback;
this.username = username;
this.password = password;
}
public static void run(@NonNull String username, @NonNull String password, @NonNull Callback callback) {
DownloadContent loader = new DownloadContent(username, password, callback);
// runs doInBackground asynchronous
loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
protected String doInBackground(String... strings) {
try {
String credentials = username + ":" + password;
URL url = new URL ("https://vertretungsplanbbscux.000webhostapp.com/auth/index.html");
// encode to 64 encoded string (necessary for authorization header)
String base64 = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
// perform a post request
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// append encoded username and password as authorization header
connection.setRequestProperty ("Authorization", "Basic " + base64);
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader (new InputStreamReader(stream));
String inputLine;
StringBuilder response = new StringBuilder();
// read html of website
while ((inputLine = reader.readLine()) != null)
response.append(inputLine);
reader.close();
return response.toString();
} catch(Exception e) {
e.printStackTrace();
}
// return null if an error has occurred
return null;
}
@Override
protected void onPostExecute(@Nullable String content) {
// called after doInBackground has finished, synchronous again
if (content == null) {
callback.onNotLoaded();
} else {
callback.onLoaded(content);
}
}
public interface Callback {
void onLoaded(String content);
void onNotLoaded();
}
}
以下是您访问内容的方式:
DownloadContent.run("fost19", "12345", new DownloadContent.Callback() {
@Override
public void onLoaded(String content) {
Log.d("DownloadContent", content);
//TODO display content in user interface
}
@Override
public void onNotLoaded() {
Log.d("DownloadContent", "could not fetch content from website");
// TODO show error message
}
});
希望我能为您的替代计划应用程序提供帮助。
Edit1:您只能使用安全连接(https)。因此,我没有使用您的.tk地址,而是使用了您网站上的实际安全地址。