我正在构建原生Android应用。我有一个不区分大小写的登录页面。
示例:密码的管理员和管理员都允许用户登录。这是不可能的。
登录正在运行。我的问题是让它区分大小写。我还是Java和Android的新手,不知道在哪里放置代码使其区分大小写。
我应该使用equalsIgnoreCase()吗?
如果是这样,你将如何在此登录中使用equalsIgnoreCase()。您在哪里使用Rest API来登录用户登录
任何帮助都会被挪用。
private class LoginProcess extends AsyncTask<Void, Void, String> {
protected void onPreExecute(){
super.onPreExecute();
dialog_login.show();
}
String user = userInput.getText().toString();
String pass = passInput.getText().toString();
@Override
protected String doInBackground(Void... voids) {
String address = my_url + "api/user/login.json";
HttpURLConnection urlConnection;
String requestBody;
Uri.Builder builder = new Uri.Builder();
Map<String, String> params = new HashMap<>();
params.put("username", user);
params.put("password", pass);
Iterator entries = params.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
builder.appendQueryParameter(entry.getKey().toString(), entry.getValue().toString());
entries.remove();
}
requestBody = builder.build().getEncodedQuery();
try {
URL url = new URL(address);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
writer.write(requestBody);
writer.flush();
writer.close();
outputStream.close();
JSONObject jsonObject = new JSONObject();
InputStream inputStream;
// get stream
if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
inputStream = urlConnection.getInputStream();
Log.w("URL", "Ok");
} else {
inputStream = urlConnection.getErrorStream();
Log.w("URL", "Bad request");
}
// parse stream
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp, response = "";
while ((temp = bufferedReader.readLine()) != null) {
response += temp;
}
// put into JSONObject
jsonObject.put("Content", response);
jsonObject.put("Message", urlConnection.getResponseMessage());
jsonObject.put("Length", urlConnection.getContentLength());
jsonObject.put("Type", urlConnection.getContentType());
JSONObject jsonObj = new JSONObject(response);
session_name = jsonObj.getString("session_name");
session_id = jsonObj.getString("sessid");
token = jsonObj.getString("token");
return jsonObject.toString();
} catch (IOException | JSONException e) {
return e.toString();
}
}
答案 0 :(得分:1)
首先,使用Basic Auth System作为登录逻辑。这应该会返回一个您将用于其他API调用的令牌。 看一下Retrofit和OkHttpClient。 此外,您的所有网络连接逻辑都应该在另一个类中。
在您的情况下,必须在服务器端测试区分大小写。 您的Android应用程序必须忘记凭据有效性。它只是从用户那里获取并将其提供给验证(或不验证)
的服务器API