我将两部分数据保存为用户密码和用户名,以便在以后的数据和随机字母组合中使用,然后检查应用程序何时再次执行以避免再次登录屏幕。
我昨天有这个工作,出于某种原因,我希望在添加功能时我放错了代码而现在无法让它再次运行,我已经整天尝试修复它,但我不能。
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 :(得分:0)
需要查看保存偏好的位置,以便我们可以将其与您的偏好设置相匹配......但是,这是一个错字吗?
public static final String PREFS_NAME = "MyPregs";
应该是“MyPrefs”而不是“MyPregs”吗?
答案 1 :(得分:0)
你有
if (SCheck1 != null && equals(UID))
但是你可能想说
if (SCheck1 != null && !SCheck.equals(UID))
答案 2 :(得分:0)
你也可以使用'反向比较'模式:
if (UID.equals(SCheck1)) {
} else {
}
这样,您不必费心检查SCheck1 == null。 UID.equals(null)将返回false而不是null.equals(UID)抛出NullPointerException。