2016-06-01 8 views
2

ここで私はログインをtriyingです。他の部分を除いてコードは正常に動作します。間違った情報を入力すると、部分とトーストはpostExecuteになります。間違ったパスワードやユーザー名を入力しても何もしません。私のコードを参照してくださいなぜ制御は他の部分には行っていないのですか?ここで私は何が欠けているのですか?提供されたログイン情報が間違っていると、制御が他の部分とToastに行かないのはなぜですか?

ここにjson有効な情報が提供されている場合。

{ 
    "status": 200, 
    "status_message": "Success", 
    "response": 
    { 
    "result": "1", 
    "memberID" : "2385", 
    "memberKey":"d69b87b2c0ad828bf7c0c30b83ea0c11" 
    } 
} 

、ここでの情報はここで間違っ

{ 
"status": "400", 
"message" : "Failure: Invalid App Key" 
} 

を提供する場合は、あなたの文はキャッチ部に行く間違った情報にlogin.code

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://mobile.betfan.com/api/?action=login"); 

        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", "[email protected]"); 

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


} 
+0

に答えています。 @ρяσѕρєяK – mejkan

+0

ユーザ名とパスが間違っている場合、どのレスポンスがサーバから 'result 'に入るのですか? –

+2

デバッガツールを使用します。いくつかのブレークポイントを入れて、関数が正しく動作していない場所を確認してください。 「{ 『 –

答えて

0

ある

if(active.equalsIgnoreCase("success")) 

のため、成功は0123を得るが失敗した場合はmessageになります。 キャッチブロックのトーストを追加します。ここ

2

はまだ解決していない

 String active = respObject.getString("status"); 



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

         session.createLoginSession("Android Hive", "[email protected]"); 

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

         finish(); 

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

     . 
        } 
+0

そのが – mejkan

+0

今私のために働い' –

+0

私のために働いてください上記の書き込みと同じコードを書きます – mejkan

関連する問題