1

I have a string with a return value, i want to compare return value with an edittext's text. but i cant do this. my code is like this :

    public static String  login (String a)
      {
            String Pass = null;
            InputStream is = null;
            String result = "";
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("UName", a));

            try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(LoginAddress);
                    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());
                Pass = "Error Connection";
            }

            //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());
                    Pass = "Convert Error";
            }
            //parse json data
            try{
                    JSONArray jArray = new JSONArray(result);
                    for(int i=0;i<jArray.length();i++){

                        JSONObject json_data = jArray.getJSONObject(i);
                            Log.i("log_tag", "UPWord: "+json_data.getString("UPWord"));
                            Pass = json_data.getString("UPWord");

                    }
            }catch(JSONException e){
                    Log.e("log_tag", "Error parsing data "+e.toString());
                    Pass = "Wrong Username or Password";
            }

            return Pass;
        }   

and i call it like this :

        public void onClick(View v)
        {
            StrictMode.setThreadPolicy(policy); 

            EditText Username = (EditText) findViewById(R.id.Usernme1);
            EditText Password = (EditText) findViewById(R.id.Passwordd1);


            String Result = DownLeftFragment.login(Username.getText().toString());

            if (Result == Password.getText().toString())
            {
                Intent intent = new Intent(MainActivity.this,HomePage.class);
                startActivity(intent);
            }
            else if (Result == "Wrong Username or Password")
            {
                Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();     
            }
            else if (Result == "Error Connection")
            {
                Toast.makeText(MainActivity.this, "Error in network connection", Toast.LENGTH_SHORT).show();        
            }
            else if (Result == "Convert Error")
            {
                Toast.makeText(MainActivity.this, "Error in app", Toast.LENGTH_SHORT).show();       
            }
            else 
            {
                Toast.makeText(MainActivity.this, Result, Toast.LENGTH_SHORT).show();       

             }

        }

i defined 2 edittexts, Username1 and passwordd1, it works fine with Username1.getText().toString(), but not with passwordd1.

(if) works fine with all of (else if)s . but not with main if (if (Result == Password.getText().toString()))

2 Answers2

2

write

If (_Password.getText().toString().trim().equals("Wrong Username or Password"))
{ 

//your code

}

else 
{
 Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();     

}
Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
0

As @Bob Malooga suggested use equals for string comparison instead of ==.

Just replace

if (Result == Password.getText().toString())
        {
            Intent intent = new Intent(MainActivity.this,HomePage.class);
            startActivity(intent);
        }
else if (Result == "Wrong Username or Password")
        {
            Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();     
        }
 else if (Result == "Error Connection")
        {
            Toast.makeText(MainActivity.this, "Error in network connection", Toast.LENGTH_SHORT).show();        
        }
 else if (Result == "Convert Error")
        {
            Toast.makeText(MainActivity.this, "Error in app", Toast.LENGTH_SHORT).show();       
        }

By

if (Result.equals(Password.getText().toString()))
        {
            Intent intent = new Intent(MainActivity.this,HomePage.class);
            startActivity(intent);
        }
else if (Result.equals("Wrong Username or Password"))
        {
            Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();     
        }
else if (Result.equals("Error Connection"))
        {
            Toast.makeText(MainActivity.this, "Error in network connection", Toast.LENGTH_SHORT).show();        
        }
else if (Result.equals("Convert Error"))
        {
            Toast.makeText(MainActivity.this, "Error in app", Toast.LENGTH_SHORT).show();       
        }
vjdhama
  • 4,878
  • 5
  • 33
  • 47