2012-04-24 11 views
1

データの2つの部分を保存して、後のデータで使用するユーザーのパスワードとユーザー名とランダムな文字の組み合わせを保存してから、再度ログインする画面が表示されないようにします。SharedPreferencesがデータを読み取っていない

私はこの機能が昨日働いていましたが、何らかの理由で、機能を追加するときにコードが間違っていて、今再び機能しないと思っています。

EDIT1:何らかの理由で、「CheckPrefs」メソッドのelse文に行きます。私は理解できません。

EDIT2:環境設定を保存して意図に移りますが、読み込めません。

コード:

public static final String PREFS_NAME = "MyPregs"; 
    public static final String PREFS_CHECK = "CheckSignedIn"; 
    public static final String UID =""; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     CheckPrefs(); 

     // Login button clicked 
     ok = (Button) findViewById(R.id.btn_login); 
     ok.setOnClickListener(this); 

     result = (TextView) findViewById(R.id.lbl_result); 

    } 
    private void CheckPrefs() { 
     // TODO Auto-generated method stub 
     //checks to see if the user has signed in before if they have it gets there data from SharedPreferences and checks against our database via HttpPost. 
     SharedPreferences settings = getSharedPreferences(PREFS_CHECK, 0); 
     String SCheck1 = settings.getString("key4", null); 
     if (SCheck1 != null && equals(UID)) { 
      Intent c = new Intent(this, CheckUserInfo.class); 
      startActivity(c); 

     } else { 

     } 



    } 
    //create bracket. 

    public void postLoginData() { 

     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 

     /* login.php returns true if username and password is equal to saranga */ 
     HttpPost httppost = new HttpPost("http://gta5news.com/login.php"); 

     try { 
      // Add user name and password 
      uname = (EditText) findViewById(R.id.txt_username); 
      String username = uname.getText().toString(); 

      pword = (EditText) findViewById(R.id.txt_password); 
      String password = pword.getText().toString(); 

      SharedPreferences signedin = getSharedPreferences(PREFS_CHECK, 0); 
      SharedPreferences.Editor editor1 = signedin.edit(); 
      editor1.putString("key4", UID); 
      editor1.commit(); 


      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
      SharedPreferences.Editor editor = settings.edit(); 
      editor.putString("key1", username); 
      editor.putString("key2", password); 
      editor.commit(); 

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("username", username)); 
      nameValuePairs.add(new BasicNameValuePair("password", password)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      Log.w("HttpPost", "Execute HTTP Post Request"); 
      HttpResponse response = httpclient.execute(httppost); 

      String str = inputStreamToString(response.getEntity().getContent()) 
        .toString(); 
      Log.w("HttpPost", str); 

      if (str.toString().equalsIgnoreCase("true")) { 
       Log.w("HttpPost", "TRUE"); 
       result.setText("Login successful"); 
       try {Thread.sleep(250); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       Intent login = new Intent(LogIn.this, ChatService.class); 
       startActivity(login); 

      } else { 
       Log.w("HttpPost", "FALSE"); 
       result.setText(str); 
      } 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private StringBuilder inputStreamToString(InputStream is) { 
     String line = ""; 
     StringBuilder total = new StringBuilder(); 
     // Wrap a BufferedReader around the InputStream 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     // Read response until the end 
     try { 
      while ((line = rd.readLine()) != null) { 
       total.append(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // Return full string 
     return total; 
    } 

    public void onClick(View view) { 
     if (view == ok) { 

      postLoginData(); 
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(pword.getWindowToken(), 0); 
     } 
     // Click end 
    } 
    // if statement 
} 

// class ends here 
+0

if(SCheck1!= null && equals(UID))?常に偽物にする –

+0

どのように?私には正しい声明のように思える。 – TheBlueCat

+0

説明を取得しようとしています。私が間違っていると私を訂正してください:key4の値を共有のprefから引き出す場合、key4の値が定数UIDと一致する場合は、IF文に行きますか?そうですか? – Gophermofur

答えて

0

は、あなたの好みを保存しているので、我々はあなたがあなたの好みを呼んでいる方法でそれを一致させることができる場所を確認する必要があります...しかし、これはタイプミスでしょうか?

public static final String PREFS_NAME = "MyPregs"; 

「MyPregs」ではなく「MyPrefs」である必要がありますか?

+0

はい、これを修正したら、私はそれを変更しようとしていました。 – TheBlueCat

0

あなた

if (SCheck1 != null && equals(UID)) 

を持っていますが、多分

if (SCheck1 != null && !SCheck.equals(UID)) 

言うことを意味していますか?

+0

いいえ?私は、UIDが共有プリファレンスにあるかどうかをチェックし、UIDと等しくないかどうかをチェックしません。 – TheBlueCat

0

は、あなたも「逆比較」パターンを使用することができます。

if (UID.equals(SCheck1)) { 

} else { 

} 

この方法で、あなたはSCheck1 == nullのチェックを気にする必要はありません。 UID.equals(null)はnull.equals(UID)の代わりにfalseを返し、NullPointerExceptionをスローします。

+0

ありがとう、私はそれを試してみます。 – TheBlueCat

+0

いいえ、全く同じことをします。 – TheBlueCat

+0

比較を行う前に、SCheck1の値が何であるかチェックしましたか?それはあなたが期待しているものですか? – Gophermofur

関連する問題