ユーザーのパスワードとユーザー名の 2 つのデータを保存して、後のデータで使用し、文字のランダムな組み合わせを保存して、アプリが再度実行されたときにチェックして、ログイン画面が再び表示されないようにします。
昨日これが機能していましたが、何らかの理由で、機能を追加するときにコードを置き忘れて、再び機能させることができないと思います.1日中修正しようとしましたが、できません.
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