2017-04-25 53 views
-1

我正在構建原生Android應用程序。我有一個不區分大小寫的登錄頁面。Android - 使應用程序登錄,區分大小寫

例子:兩個密碼的管理員管理允許用戶登錄。這應該是不可能的。

登錄正在工作。我的問題是區分大小寫。我對Java和Android還是一個新手,並不確定在哪裏放置代碼以區分大小寫。

我應該使用equalsIgnoreCase()嗎?
如果是這樣,你將如何在這個登錄中使用equalsIgnoreCase()。你在哪裏主持用戶登錄與其他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(); 
     } 
    } 
+1

我沒有看到任何關於密碼的檢查,所以我不知道這是否是Android/Java的問題。 – Denny

+1

你不覺得這個問題是在服務器端嗎? –

+0

我認爲你是對的。感謝您的回覆 –

回答

1

首先,爲您的登錄邏輯使用基本身份驗證系統。這應該會將您將用於其他API調用的令牌發回給您。 看看Retrofit和OkHttpClient。 此外,您的所有網絡連接邏輯應該在另一類。

在你的情況下,必須在服務器端測試大小寫敏感性。 您的android應用程序必須對憑證有效性不知情。它只是把它從用戶,並提供給服務器API驗證(或不)

+0

謝謝,這非常有幫助。 –