0

我创建了一个简单的应用程序,它将登录到数据库并从中获取数据。但是现在我发现了一个问题:如果你第一次写错了登录数据,第二次尝试登录就不行了。你必须重新启动它。

我认为这段代码有问题:EDITED

public class MyMoodleApplicationActivity extends Activity {
/** Called when the activity is first created. */
EditText username;
EditText password;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    username = (EditText)findViewById(R.id.username);
    password = (EditText)findViewById(R.id.password);
    final Button loginButton = (Button)findViewById(R.id.login);      
    loginButton.setOnClickListener(loginListener);
    final Button clearButton = (Button)findViewById(R.id.clear);
    clearButton.setOnClickListener(clearListener);
}
private OnClickListener loginListener = new OnClickListener(){

    public void onClick(View v){
        String usr = username.getText().toString();
        String psw = password.getText().toString();
        System.out.println("Username: "+usr);
        System.out.println("Password: "+psw);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("usern",""+usr));
        nameValuePairs.add(new BasicNameValuePair("passw",""+psw));
        InputStream is = null; 
        String result = "";
        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://ik.su.lt/*****");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
        //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();

                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }
        String usernameFromDB = "";
        String firstnameFromDB = "";
        String lastnameFromDB = "";
        String emailFromDB = "";
        String phoneFromDB = "";
        String skypeFromDB = "";
        String cityFromDB = "";
        String descriptionFromDB = "";
        try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        usernameFromDB = json_data.getString("username");
                        firstnameFromDB = json_data.getString("firstname");
                        lastnameFromDB = json_data.getString("lastname");
                        emailFromDB = json_data.getString("email");
                        phoneFromDB = json_data.getString("phone1");
                        skypeFromDB = json_data.getString("skype");
                        cityFromDB = json_data.getString("city");
                        descriptionFromDB = json_data.getString("description");

                        System.out.println(usernameFromDB+ "  " + firstnameFromDB+"  "+lastnameFromDB+"  "
                                + emailFromDB + "  " + phoneFromDB +"  " + skypeFromDB+ "  " + cityFromDB + "  "+
                                descriptionFromDB);
                }
        }
        catch(JSONException e){
            AlertDialog alertDialog = new AlertDialog.Builder(MyMoodleApplicationActivity.this).create();
            alertDialog.setTitle("Klaida!");
            alertDialog.setMessage("Toks vartotojas neegzistuoja");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    return;
                }
            });
            alertDialog.show();
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
        if(usr.length()== 0){
            AlertDialog alertDialog = new AlertDialog.Builder(MyMoodleApplicationActivity.this).create();
            alertDialog.setTitle("Klaida!");
            alertDialog.setMessage("Jūs neįvedėte slapyvardžio");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    return;
                }
            });
            alertDialog.show();
        }
        else if(psw.length()==0){
            AlertDialog alertDialog = new AlertDialog.Builder(MyMoodleApplicationActivity.this).create();
            alertDialog.setTitle("Klaida!");
            alertDialog.setMessage("Jūs neįvedėte slaptažodžio");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    return;
                }
            });
            alertDialog.show();
        }
        else if (usr.equals(usernameFromDB)){
            Intent in = new Intent(getApplicationContext(), LoggedUser.class);
            in.putExtra("firstname", firstnameFromDB);
            in.putExtra("lastname", lastnameFromDB);
            in.putExtra("email", emailFromDB);
            in.putExtra("phone1", phoneFromDB);
            in.putExtra("skype", skypeFromDB);
            in.putExtra("city", cityFromDB);
            in.putExtra("description", descriptionFromDB);

            startActivity(in);
            finish();
    }
}};

private OnClickListener clearListener = new OnClickListener(){
    @Override
    public void onClick(View v){
        username.setText("");
        password.setText("");
    }
};

在这些 if 语句中,我检查您输入的用户名和密码是否为空用户名不等于数据库中的用户名。如何重写它可以工作的代码?

已编辑: 我在 DB 中有用户 john,但如果我在登录字段中写入 John 并按下登录按钮,则不会说没有 John 用户。在 LogCat 中,我看到读取了 json 流。有没有办法设置大写和小写字母不同的规则?

4

0 回答 0