java - SharedPreferences不读取数据

标签 java android sharedpreferences preferences

我保存了两部分数据,即用户密码和用户名以供以后使用,以及随机的字母组合,以便在应用程序再次执行时进行检查,以避免再次出现登录屏幕。

我昨天已经完成了这个工作,由于某种原因,我希望在添加功能时我放错了代码,现在无法让它再次工作,我已经尝试了一整天来修复它,但我不能。

EDIT1:由于某种原因,它转到“CheckPrefs”方法上的 else 语句,我无法理解。

EDIT2:它可以很好地保存首选项并转到 Intent ,但是它只是无法读取它。

代码:

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

最佳答案

需要查看您保存偏好设置的位置,以便我们可以将其与您调用偏好设置的方式进行匹配...但是,这是一个拼写错误吗?

public static final String PREFS_NAME = "MyPregs"; 

应该是“MyPrefs”而不是“MyPregs”吗?

关于java - SharedPreferences不读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10304163/

相关文章:

java - android.permission.BATTERY_STATS 用法

android - SharedPreference 值的 Kotlin 扩展字段(按字符串常量键)

java - 从 Tomcat 使用 WebDriver

java - 使用java中的循环绘制多个矩形,并进行碰撞检测,如果发生碰撞,则更改其x和y坐标(随机)。

java - 为什么别名在 java 方法中不起作用?

android - OnSharedPreferenceChangeListener 不适用于所有设备?

android - 使用共享首选项创建最喜欢的 ListView

java - 我是否需要为每个请求创建一个新的 Struts Action 类?

android - 将 Android 设备型号映射到人类可读的品牌名称

java - 如何将一侧边框和背景添加到一个可绘制对象中?