二回

2017-12-03 9 views
0

Imは、基本的なログインアプリケーションを作るStringRequestを行うことはできません、ショーの条件以下のこのコードは、私は私が作るとき二回

private void checkLogin(final String username, final String password) { 
    pDialog = new ProgressDialog(this); 
    pDialog.setCancelable(false); 
    pDialog.setMessage("Logging in ..."); 
    showDialog(); 
    txt_username.setText(""); 
    txt_password.setText(""); 

    StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 

     @Override 
     public void onResponse(String response) { 
      Log.e(TAG, "Login Response: " + response.toString()); 
      hideDialog(); 

      try { 
       JSONObject jObj = new JSONObject(response); 
       success = jObj.getInt(TAG_SUCCESS); 

       // Check for error node in json 
       if (success == 1) { 
        String username = jObj.getString(TAG_USERNAME); 
        String id = jObj.getString(TAG_ID); 

        Log.e("Successfully Login!", jObj.toString()); 

        Toast.makeText(getApplicationContext(), jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show(); 

        // menyimpan login ke session 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        editor.putBoolean(session_status, true); 
        editor.putString(TAG_ID, id); 
        editor.putString(TAG_USERNAME, username); 
        editor.commit(); 

        // Memanggil main activity 
        Intent intent = new Intent(Login.this, MainActivity.class); 
        intent.putExtra(TAG_ID, id); 
        intent.putExtra(TAG_USERNAME, username); 
        finish(); 
        startActivity(intent); 
       } else { 
        Toast.makeText(getApplicationContext(), 
          jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show(); 

       } 
      } catch (JSONException e) { 
       // JSON error 
       e.printStackTrace(); 
      } 

     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e(TAG, "Login Error: " + error.getMessage()); 
      Toast.makeText(getApplicationContext(), 
        error.getMessage(), Toast.LENGTH_LONG).show(); 

      hideDialog(); 

     } 
    }) { 

     @Override 
     protected Map<String, String> getParams() { 
      // Posting parameters to login url 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("username", username); 
      params.put("password", password); 

      return params; 
     } 
    }; 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(strReq, tag_json_obj); 
} 

は、私は何の問題もないcheckloginの手順については、次にログインボタン

btn_login.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       final String username = txt_username.getText().toString(); 
       final String password = txt_password.getText().toString(); 
       // mengecek kolom yang kosong 
       if (username.trim().length() > 0 && password.trim().length() > 0) { 
        if (conMgr.getActiveNetworkInfo() != null 
          && conMgr.getActiveNetworkInfo().isAvailable() 
          && conMgr.getActiveNetworkInfo().isConnected()) { 
         checkLogin(username, password); 
        } else { 
         Toast.makeText(getApplicationContext() ,"No Internet Connection", Toast.LENGTH_LONG).show(); 
        } 
       } else { 
        // Prompt user to enter credentials 
        Toast.makeText(getApplicationContext() ,"Kolom tidak boleh kosong", Toast.LENGTH_LONG).show(); 
       } 
      } 
     });` 

をクリックすると、最初のログインクリック、それは私のサーバー内のPHPファイルを読み取ることができます要求を行うが、私は2回目をクリックすると、要求を行うことはできませんとマッサージのログを表示していない、私のアプリは、

答えて

0

まず、デフォルトでVolleyが自動的にリクエストをキャッシュすることに言及しておきます。そのため

一つの解決策は次のとおりです。

// Create your request 
StringRequest strReq = new StringRequest(Request.Method.POST....... 

// Disable the cache option before you add it to the queue. 
strReq.setShouldCache(false); 

// Adding request to request queue 
AppController.getInstance().addToRequestQueue(strReq, tag_json_obj); 
+0

すごい –

+0

その作業たくさんおかげで、私はちょうど無効キャッシュXD –

+0

を必要とする正しいものとして答えをマークしてください。 – motis10