mardi 31 mai 2016

Why Control not goes in Else part and Toast, if login information provide wrong?

Here i Am triying to login all code work fine except else part when i provide wrong information it should goes else part and toast in postExecute. but it does not do anything when i provide wrong password or username. please see my code why control not goes in else part? what i am missing here?

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(Login .this, "Please wait", "Loading...");
            }

            @Override
            protected String doInBackground(String... params) {


                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.accumulate("username", params[0]);
                } catch (JSONException e4) {
                    // TODO Auto-generated catch block
                    e4.printStackTrace();
                }
                try {
                    jsonObject.accumulate("password", params[1]);
                } catch (JSONException e3) {
                    // TODO Auto-generated catch block
                    e3.printStackTrace();
                }
                try {
                    jsonObject.accumulate("deviceToken", "2324h5gj345gj3hs4g5j34g");
                } catch (JSONException e5) {
                    // TODO Auto-generated catch block
                    e5.printStackTrace();
                }
                try {
                    jsonObject.accumulate("os", "android");
                } catch (JSONException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                try {
                    jsonObject.accumulate("key", "MEu07MgiuWgXwJOo7Oe1aHL0ayM8VvP");
                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }


                // 4. convert JSONObject to JSON to String
                String dataString = jsonObject.toString();


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

                try{
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(
                            "http://ift.tt/24qaOb1");

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




    //Here control not goes else part if  password or username invalid

            protected void onPostExecute(String result){
                String s = result.trim();
                loadingDialog.dismiss();

                    JSONObject respObject;
                    try {
                        respObject = new JSONObject(s);
                        String active = respObject.getString("status_message");



                        if(active.equalsIgnoreCase("success")){
                            Toast.makeText(getApplicationContext(), s+"Login successfull", Toast.LENGTH_LONG).show();

                            session.createLoginSession("Android Hive", "anroidhive@gmail.com");

                            Intent intent=new Intent(Login.this,Welcome.class);
                            startActivity(intent);

                            finish();

                        }else {
                            Toast.makeText(getApplicationContext(), "Login Fail", Toast.LENGTH_LONG).show();

          //Control not come here if information provide wrong and not toast.
                        }

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



            }


        }

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


}

Aucun commentaire:

Enregistrer un commentaire