2014-06-10 103 views
0

我想顯示該用戶名和密碼不匹配,如果我填寫了錯誤的用戶名和密碼。在按鈕上點擊正確的名稱和密碼即可正常工作,並顯示消息登錄成功,但其他條件不起作用。我錯在哪裏?告訴我如何解決此問題並顯示消息用戶名或密碼不匹配。在Android中登錄錯誤

LogIn.java

private class SigIn extends AsyncTask<String, String, String[]> { 
       private final Context _context; 

       public SigIn(Context context){ 
        _context = context; 

       } 
      ProgressDialog pDialog = new ProgressDialog(LoginActivity.this); 
      @SuppressWarnings("deprecation") 
      @SuppressLint("WorldReadableFiles") 
      @Override 
      protected String[] doInBackground(final String... params) 
      { 
       ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
       if (conMgr.getActiveNetworkInfo() != null 
         && conMgr.getActiveNetworkInfo().isAvailable() 
         && conMgr.getActiveNetworkInfo().isConnected()) 
       { 
        HttpClient httpclient = new DefaultHttpClient(); 
        try 
        { 
         pDialog.setMessage("Please wait signing in..."); 
         runOnUiThread(new Runnable() 
         { 
          public void run() 
          { 
           pDialog.show(); 
          } 
         }); 

         JSONObject job= new JSONObject(); 
         euid = uedit.getText().toString(); 
         epass = pedit.getText().toString(); 

         euid.replace("" ,"%20"); 
         epass.replace("" ,"%20"); 
         job.put("status_key","2"); 
         job.put("method", "login"); 
         job.put("email", euid); 
         job.put("password",epass); 



        /* 
         System.out.print("latitude is "+latitude); 
         job.put("latitude",latitude); 
         job.put("longitude",longitude);*/ 

         StringEntity se = new StringEntity(job.toString()); 

         HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php"); 

         httppost.setEntity(se); 

         HttpResponse response = httpclient.execute(httppost); 
          String data = EntityUtils.toString(response.getEntity()); 
          System.out.println("response "+data); 

          JSONObject jo = new JSONObject(data); 

         Log.d("response", jo.toString(4)); 

         JSONArray jArray = jo.getJSONArray("driver_details"); 
         Log.d("Array", jArray.toString(4)); 
         JSONObject adv= jo.getJSONObject("advertisement_count"); 
         advertisement_count=adv.getString("count(*)"); 
         System.out.print("advertisemnet"+advertisement_count);    
         for (int i=0; i < jArray.length(); i++) 
         { 
          try { 
          JSONObject jsob = jArray.getJSONObject(i); 

            mfname = jsob.getString("first_name"); 
            msname = jsob.getString("Surname"); 
            mun = jsob.getString("unit_no"); 
            mstreet = jsob.getString("street_name"); 
            msuburb = jsob.getString("Suburb"); 
            mstate = jsob.getString("State"); 
            mpassword = jsob.getString("password"); 
            mpostcode = jsob.getString("post_code"); 
            mlicense = jsob.getString("driving_lic"); 
            mid = jsob.getString("authority_id"); 
            mmobile = jsob.getString("mobile_no"); 
            memail = jsob.getString("Email"); 
            login_token=jsob.getString("login_token"); 
            mimagelicense=jsob.getString("license_pic"); 
            mauthcard=jsob.getString("audit_card_pic"); 
            mpic=jsob.getString("face_pic"); 
    } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
    } 
    pref=getSharedPreferences("Driver", MODE_WORLD_READABLE); 
         SharedPreferences.Editor edit=pref.edit(); 

         edit.putString("mfname", mfname); 
         edit.putString("msname", msname); 
         edit.putString("mun", mun); 
         edit.putString("mstreet", mstreet); 
         edit.putString("msuburb", msuburb); 
         edit.putString("mpassword", mpassword); 
         edit.putString("mpostcode", mpostcode); 
         edit.putString("mlicense", mlicense); 
         edit.putString("mid", mid); 
         edit.putString("mmobile", mmobile); 
         edit.putString("memail", memail); 
         edit.putString("login_token", login_token); 
         edit.putString("mimagelicense", mimagelicense); 
         edit.putString("mauthcard", mauthcard); 
         edit.putString("mpic", mpic); 
         edit.putString("advertisement_count", advertisement_count); 
         edit.commit(); 

     if(jo.getString("err-code").equals("0")) 
          { 
           final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this); 
           alert.setTitle("Login !!!"); 
           //alert.setTitle("Login unsuccessfull !"); 
          alert.setMessage(jo.getString("message")); 
           //alert.setMessage("Username and password does not match."); 
           alert.setPositiveButton("Ok", 
             new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, 
              int whichButton) 
            { 
             pDialog.dismiss(); 
             dialog.dismiss(); 

             /*Intent tabint = new Intent(getApplicationContext(),agreement.class); 
               startActivity(tabint);*/ 
             Intent tabint = new Intent(getApplicationContext(),agreement.class); 
              startActivity(tabint); 
           } 
           }); 
           runOnUiThread(new Runnable() { 
            public void run() { 
             alert.show(); 
            } 
           }); 
          } 

          else if(jo.getString("err-code").equals("300")) 
          { 
           final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this); 
           alert.setTitle("Login!!!"); 
           alert.setMessage(jo.getString("message")); 
           alert.setPositiveButton("Ok", 
             new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, 
              int whichButton) 
            { 
             pDialog.dismiss(); 
             dialog.dismiss(); 

             //Intent tabint = new Intent(getApplicationContext(),agreement.class); 
              // startActivity(tabint); 
           } 
           }); 
           runOnUiThread(new Runnable() { 
            public void run() { 
             alert.show(); 
            } 
           }); 
          } 
          else 
          { 

           final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this); 
           alert.setTitle("Alert !"); 
           alert.setMessage(jo.getString("message")); 
           alert.setPositiveButton("Ok", 

            new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, 
              int whichButton) { 
             dialog.dismiss(); 

            } 
           }); 
           runOnUiThread(new Runnable() 
           { 
            public void run() 
            { 
             pDialog.dismiss(); 

             alert.show(); 
            } 
           }); 
          } 


         } 
         catch (Exception e) 
         { 
          e.printStackTrace(); 
         } 
        } 
        return params; 
       } 

不同的用戶名和密碼登錄後,它會給我的錯誤日誌貓一樣:

-10 03:41:20.550: W/System.err(32253): org.json.JSONException: No value for driver_details 
    06-10 03:41:20.560: W/System.err(32253): at org.json.JSONObject.get(JSONObject.java:355) 
    06-10 03:41:20.560: W/System.err(32253): at org.json.JSONObject.getJSONArray(JSONObject.java:549) 
    06-10 03:41:20.560: W/System.err(32253): at com.sunmobileappnow.mobileappnow.LoginActivity$SigIn.doInBackground(LoginActivity.java:263) 
    06-10 03:41:20.560: W/System.err(32253): at com.sunmobileappnow.mobileappnow.LoginActivity$SigIn.doInBackground(LoginActivity.java:1) 
    06-10 03:41:20.560: W/System.err(32253): at android.os.AsyncTask$2.call(AsyncTask.java:288) 
    06-10 03:41:20.560: W/System.err(32253): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    06-10 03:41:20.560: W/System.err(32253): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
    06-10 03:41:20.570: W/System.err(32253): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    06-10 03:41:20.570: W/System.err(32253): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
+1

o rg.json.JSONException:沒有driver_details的值, –

+0

http://www.json.org/javadoc/org/json/JSONObject.html#getJSONArray(java.lang.String) – ben75

+0

我該如何解決這個問題。 – user3725779

回答

0

把你來自哪裏,JSON獲取數據的代碼,如果這樣的條件存儲在共享prefrence裏面..

    //preciding codes here... 

       JSONObject jo = new JSONObject(data); 

       Log.d("response", jo.toString(4)); 

       if(jo.getString("err-code").equals("0")) 
       { 
        JSONArray jArray = jo.getJSONArray("driver_details"); 
        Log.d("Array", jArray.toString(4)); 
        JSONObject adv= jo.getJSONObject("advertisement_count"); 
        advertisement_count=adv.getString("count(*)"); 
        System.out.print("advertisemnet"+advertisement_count);    
        for (int i=0; i < jArray.length(); i++) 
        { 
         try { 
           JSONObject jsob = jArray.getJSONObject(i); 

           mfname = jsob.getString("first_name"); 
           msname = jsob.getString("Surname"); 
           mun = jsob.getString("unit_no"); 
           mstreet = jsob.getString("street_name"); 
           msuburb = jsob.getString("Suburb"); 
           mstate = jsob.getString("State"); 
           mpassword = jsob.getString("password"); 
           mpostcode = jsob.getString("post_code"); 
           mlicense = jsob.getString("driving_lic"); 
           mid = jsob.getString("authority_id"); 
           mmobile = jsob.getString("mobile_no"); 
           memail = jsob.getString("Email"); 
           login_token=jsob.getString("login_token"); 
           mimagelicense=jsob.getString("license_pic"); 
           mauthcard=jsob.getString("audit_card_pic"); 
           mpic=jsob.getString("face_pic"); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 

        pref=getSharedPreferences("Driver", MODE_WORLD_READABLE); 
        SharedPreferences.Editor edit=pref.edit(); 

        edit.putString("mfname", mfname); 
        edit.putString("msname", msname); 
        edit.putString("mun", mun); 
        edit.putString("mstreet", mstreet); 
        edit.putString("msuburb", msuburb); 
        edit.putString("mpassword", mpassword); 
        edit.putString("mpostcode", mpostcode); 
        edit.putString("mlicense", mlicense); 
        edit.putString("mid", mid); 
        edit.putString("mmobile", mmobile); 
        edit.putString("memail", memail); 
        edit.putString("login_token", login_token); 
        edit.putString("mimagelicense", mimagelicense); 
        edit.putString("mauthcard", mauthcard); 
        edit.putString("mpic", mpic); 
        edit.putString("advertisement_count", advertisement_count); 
        edit.commit(); 

        final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this); 
          alert.setTitle("Login !!!"); 
          //alert.setTitle("Login unsuccessfull !"); 
         alert.setMessage(jo.getString("message")); 
          //alert.setMessage("Username and password does not match."); 
          alert.setPositiveButton("Ok", 
            new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int whichButton) 
           { 
            pDialog.dismiss(); 
            dialog.dismiss(); 

            /*Intent tabint = new Intent(getApplicationContext(),agreement.class); 
              startActivity(tabint);*/ 
            Intent tabint = new Intent(getApplicationContext(),agreement.class); 
             startActivity(tabint); 
          } 
          }); 
          runOnUiThread(new Runnable() { 
           public void run() { 
            alert.show(); 
           } 
          }); 
        } 



        else if(jo.getString("err-code").equals("300")) 
        { 
         //reamining codes... 
+0

我已經創建了兩項服務 – user3725779

+0

向我們展示了在輸入錯誤憑證時得到的回覆。 –

+0

不是你的api url:http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php ?? –