2016-07-31 69 views
0

我試圖登錄並轉到另一個新頁面,但問題是,單擊按鈕後,我收到消息:「無效的用戶名或密碼」。彈出消息「無效的用戶名或密碼」

我不知道發生了什麼,還搜索了一些示例教程來解決我的問題,但仍然找不到答案。

這裏是我的代碼:

public class MainActivity extends ActionBarActivity { 
private EditText editTextUserName; 
private EditText editTextPassword; 

public static final String USER_NAME = "USERNAME"; 

String username; 
String password; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    editTextUserName = (EditText) findViewById(R.id.editTextUserName); 
    editTextPassword = (EditText) findViewById(R.id.editTextPassword); 
} 

public void invokeLogin(View view){ 
    username = editTextUserName.getText().toString(); 
    password = editTextPassword.getText().toString(); 

    login(username,password); 

} 

private void login(final String username, String password) { 

    class LoginAsync extends AsyncTask<String, Void, String>{ 

     private Dialog loadingDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading..."); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      String uname = params[0]; 
      String pass = params[1]; 

      InputStream is = null; 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("username", uname)); 
      nameValuePairs.add(new BasicNameValuePair("password", pass)); 
      String result = null; 

      try{ 
       HttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(
         "http://192.168.1.101/test/read_allorder.php"); 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       HttpResponse response = httpClient.execute(httpPost); 

       HttpEntity entity = response.getEntity(); 

       is = entity.getContent(); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       result = sb.toString(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return result; 
     } 

     @Override 
     protected void onPostExecute(String result){ 
      String s = result.trim(); 
      loadingDialog.dismiss(); 
      if(s.equalsIgnoreCase("success")){ 
       Intent intent = new Intent(MainActivity.this, UserProfile.class); 
       intent.putExtra(USER_NAME, username); 
       finish(); 
       startActivity(intent); 
      }else { 
       Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 

    LoginAsync la = new LoginAsync(); 
    la.execute(username, password); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

+0

你至少應該告訴你正在使用的身份驗證工具/服務,並分享其代碼。 – Shaishav

+0

您的doInBackground不會返回成功,並且您在onPostExecute中檢查成功,如果沒有成功,則跳轉到其他位置並返回您無效的用戶名或密碼 找到它在doInBackground中返回不成功的原因 –

+0

發佈相關HTML部分服務器顯示的登錄頁面。 – Robert

回答

0

您正在嘗試這一點 -

while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 

如果結果等於成功,然後進入下一個活動,但結果都不可能什麼。

但是,如果你要到下一個活動取代

result = sb.toString(); 

result = "success" 
+0

非常感謝您的幫助,但問題一旦改爲「成功」,我的應用可以接受任何用戶名和密碼,意味着任何人都可以在我的應用中登錄,爲什麼? – Ziha

相關問題