弹出消息"无效的用户名或密码"

时间:2016-07-31 15:02:57

标签: android

我尝试登录并转到另一个新页面,但问题是,点击按钮后,我收到消息:"无效的用户名或密码"。

我不知道发生了什么,也搜索了一些示例教程来解决我的问题,但仍然找不到答案。

这是我的代码:

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);
}

}

1 个答案:

答案 0 :(得分:0)

你正在尝试这个 -

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

如果结果等于成功,那么转到下一个活动但结果可能是任何事情。

但是如果你想进入下一个活动替换

result = sb.toString();

result = "success"