- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的问题是这个问题的扩展 here但在这个问题中我添加了两个函数:
目前,我设法在有/没有记住我复选框的情况下登录,并打算从 mysql 数据库中获取其余的 JSON 对象。
但问题出在 LoginActivity.java PART A block 内部。当我重新启动/重建应用程序时, Intent 数据为空,不会存储并发送到下一个 Activity 。自动登录时,数据无法存储和发送到下一个 Activity 。代码如下
JSON 对象
{
"access":"PA001",
"password":"123",
"fullname":"ARCADE",
"branch":"HQ",
"section":"MPR"
}
access.php
<?php
$conn = mysqli_connect("","","","");
if(
isset($_POST['access']) &&
isset($_POST['password'])
){
$access = $_POST['access'];
$password = $_POST['password'];
$sql = "SELECT * FROM table WHERE access = '$access' AND password = '$password' ";
$result = mysqli_query($conn, $sql);
if($result && mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$accessdb = $row['access'];
$passworddb = $row['password'];
$fullnamedb = $row['fullname'];
$branchdb = $row['branch'];
$sectiondb = $row['section'];
echo "success_access";
$response = array('access' => $accessdb, 'password' => $passworddb, 'fullname' => $fullnamedb, 'branch' => $branchdb, 'section' => $sectiondb);
echo json_encode($response);
}
mysqli_free_result($result);
} else {
echo "access_failed";
}
}
?>
LoginActivity.java
public class LoginActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
final String TAG = this.getClass().getName();
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
EditText etLogin, etPassword;
CheckBox cbRememberMe;
Button bLogin;
boolean checkRememberMe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etLogin = (EditText)findViewById(R.id.etLogin);
etPassword = (EditText)findViewById(R.id.etPassword);
cbRememberMe = (CheckBox)findViewById(R.id.cbRememberMe);
cbRememberMe.setOnCheckedChangeListener(this);
checkRememberMe = cbRememberMe.isChecked();
sharedPreferences = getSharedPreferences("login.conf", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
//////////////////////////////////////// PART A /////////////////////////////////////
final String accessdb = sharedPreferences.getString("access", "");
final String passworddb = sharedPreferences.getString("password", "");
final String fullnamedb = sharedPreferences.getString("fullname", "");
final String branchdb = sharedPreferences.getString("branch", "");
final String sectiondb = sharedPreferences.getString("branch", "");
final HashMap data = new HashMap();
data.put("access", accessdb);
data.put("password", passworddb);
data.put("fullname", fullnamedb);
data.put("branch", branchdb);
data.put("section", sectiondb);
if(!(accessdb.contains("") && passworddb.contains("") && fullnamedb.contains("") && branchdb.contains("") && sectiondb.contains(""))){
PostResponseAsyncTask task = new PostResponseAsyncTask(LoginActivity.this, data, new AsyncResponse() {
@Override
public void processFinish(String s) {
// edited here ,add Log
Log.d(TAG, "processFinish : " + s);
String responseToJSONObject = s.substring(s.indexOf("{"));
if(s.contains("success_access")){
try {
JSONObject jsonObject = new JSONObject(responseToJSONObject);
final String logindb = jsonObject.getString("login");
final String pwdb = jsonObject.getString("pw");
final String realnamedb = jsonObject.getString("real_name");
final String deptdb = jsonObject.getString("dept");
Intent intent = new Intent(LoginActivity.this, NextActivity.class);
intent.putExtra("login", logindb);
intent.putExtra("pw", pwdb);
intent.putExtra("real_name", realnamedb);
intent.putExtra("dept", deptdb);
LoginActivity.this.startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
task.execute("http://localhost/login.php");
}
//////////////////////////////////////// PART A /////////////////////////////////////
bLogin = (Button)findViewById(R.id.bLogin);
bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://localhost/login.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// edited here ,add Log
Log.d(TAG, "onResponse : " + response);
if(response.contains("success_access")){
String resp = response.substring(response.indexOf("{"));
// LOGIN WITH CHECK REMEMBER ME
if(checkRememberMe){
try {
JSONObject jsonObject = new JSONObject(resp);
final String accessdb = jsonObject.getString("access");
final String passworddb = jsonObject.getString("password");
final String fullnamedb = jsonObject.getString("fullname");
final String branchdb = jsonObject.getString("branch");
final String sectiondb = jsonObject.getString("section");
editor.putString("access", etLogin.getText().toString());
editor.putString("password", etPassword.getText().toString());
editor.putString("fullname", fullnamedb);
editor.putString("branch", branchdb);
editor.putString("section", sectiondb);
editor.putBoolean("isLoggedIn", true);
editor.apply();
Intent intent = new Intent(LoginActivity.this, NextActivity.class);
intent.putExtra("access", accessdb);
intent.putExtra("password", passworddb);
intent.putExtra("fullname", fullnamedb);
intent.putExtra("branch", branchdb);
intent.putExtra("section", sectiondb);
LoginActivity.this.startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
// LOGIN WITH CHECK REMEMBER ME
// LOGIN WITHOUT CHECK REMEMBER ME
else {
try {
JSONObject jsonObject = new JSONObject(resp);
final String accessdb = jsonObject.getString("access");
final String passworddb = jsonObject.getString("password");
final String fullnamedb = jsonObject.getString("fullname");
final String branchdb = jsonObject.getString("branch");
final String sectiondb = jsonObject.getString("section");
Intent intent = new Intent(LoginActivity.this, NextActivity.class);
intent.putExtra("access", accessdb);
intent.putExtra("password", passworddb);
intent.putExtra("fullname", fullnamedb);
intent.putExtra("branch", branchdb);
intent.putExtra("section", sectiondb);
LoginActivity.this.startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
// LOGIN WITHOUT CHECK REMEMBER ME
} else{
Toast.makeText(getApplicationContext(), "Error" , Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error" , Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("login", etLogin.getText().toString());
params.put("pw", etPassword.getText().toString());
return params;
}
};
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
}
});
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
checkRememberMe = isChecked;
Log.d(TAG, "Remember me is = " + checkRememberMe);
}
}
编辑:
我的日志
08-22 16:50:28.802 21022-21022/? I/art: Not late-enabling -Xcheck:jni (already on)
08-22 16:50:28.802 21022-21022/? W/art: Unexpected CPU variant for X86 using defaults: x86
08-22 16:50:28.822 21022-21029/? I/art: Debugger is no longer active
08-22 16:50:28.822 21022-21029/? I/art: Starting a blocking GC Instrumentation
08-22 16:50:28.895 21022-21022/? W/System: ClassLoader referenced unknown path: /data/app/com.app.test-2/lib/x86
08-22 16:50:28.901 21022-21022/? I/InstantRun: starting instant run server: is main process
08-22 16:50:29.129 21022-21040/? I/OpenGLRenderer: Initialized EGL, version 1.4
08-22 16:50:29.129 21022-21040/? D/OpenGLRenderer: Swap behavior 1
08-22 16:50:29.130 21022-21040/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-22 16:50:29.130 21022-21040/? D/OpenGLRenderer: Swap behavior 0
08-22 16:50:29.133 21022-21040/? D/EGL_emulation: eglCreateContext: 0xb3305060: maj 2 min 0 rcv 2
08-22 16:50:29.150 21022-21040/? D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303360)
08-22 16:50:29.156 21022-21040/? D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303360)
08-22 16:50:29.964 21022-21022/com.app.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-22 16:50:30.037 21022-21022/com.app.test W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
08-22 16:50:30.040 21022-21040/com.app.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303360)
08-22 16:50:44.279 21022-21022/com.app.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
08-22 16:50:46.163 21022-21022/com.app.test D/com.app.test.LoginActivity: Check flag is = true
08-22 16:50:47.820 21022-21323/com.app.test D/NetworkSecurityConfig: No Network Security Config specified, using platform default
// LOG.d after i logged in with remember me check in
08-22 16:50:47.824 21022-21022/com.app.test E/com.app.test.LoginActivity: Response is = success_access{"access":"ID001","password":"1233","fullname":"ARCADE","branch":"HQ", "section":"MPR"}
08-22 16:50:47.825 21022-21022/com.app.test E/com.app.test.LoginActivity: JSON Object is = {"access":"ID001","password":"1233","fullname":"ARCADE","branch":"HQ", "section":"MPR"}
08-22 16:50:47.825 21022-21022/com.app.test D/com.app.test.LoginActivity: ID001
08-22 16:50:47.825 21022-21022/com.app.test D/com.app.test.LoginActivity: 123
08-22 16:50:47.982 21022-21025/com.app.test I/art: Do partial code cache collection, code=29KB, data=30KB
08-22 16:50:47.982 21022-21025/com.app.test I/art: After code cache collection, code=29KB, data=30KB
08-22 16:50:47.982 21022-21025/com.app.test I/art: Increasing code cache capacity to 128KB
08-22 16:50:47.994 21022-21040/com.app.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303360)
08-22 16:50:48.083 21022-21040/com.app.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303360)
08-22 16:50:48.100 21022-21040/com.app.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303360)
08-22 16:50:48.135 21022-21022/com.app.test W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
// LOG.d when I restarted apps without logout.
08-22 16:54:32.607 24710-24710/? I/art: Not late-enabling -Xcheck:jni (already on)
08-22 16:54:32.607 24710-24710/? W/art: Unexpected CPU variant for X86 using defaults: x86
08-22 16:54:32.694 24710-24710/com.app.test W/System: ClassLoader referenced unknown path: /data/app/com.app.test-2/lib/x86
08-22 16:54:32.699 24710-24710/com.app.test I/InstantRun: starting instant run server: is main process
08-22 16:54:34.256 24710-24811/com.app.test I/OpenGLRenderer: Initialized EGL, version 1.4
08-22 16:54:34.257 24710-24811/com.app.test D/OpenGLRenderer: Swap behavior 1
08-22 16:54:34.257 24710-24811/com.app.test W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-22 16:54:34.257 24710-24811/com.app.test D/OpenGLRenderer: Swap behavior 0
08-22 16:54:34.258 24710-24811/com.app.test D/EGL_emulation: eglCreateContext: 0xb3305360: maj 2 min 0 rcv 2
08-22 16:54:34.266 24710-24811/com.app.test D/EGL_emulation: eglMakeCurrent: 0xb3305360: ver 2 0 (tinfo 0xb3303200)
08-22 16:54:34.274 24710-24811/com.app.test D/EGL_emulation: eglMakeCurrent: 0xb3305360: ver 2 0 (tinfo 0xb3303200)
08-22 16:54:35.124 24710-24710/com.app.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-22 16:54:35.292 24710-24710/com.app.test W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
08-22 16:54:35.299 24710-24811/com.app.test D/EGL_emulation: eglMakeCurrent: 0xb3305360: ver 2 0 (tinfo 0xb3303200)
感谢有人能提供帮助。谢谢。
最佳答案
试试这个。
public class SharedPreferenceUtils {
private static final String SP_NAME = "sp";
public static final String ACCESS = "access";
public static final String FULL_NAME = "fullname";
public static final String BRANCH = "branch";
public static final String SECTION = "section";
public static final String IS_LOGGED_IN = "isLoggedIn";
// create
public static boolean createSP(Context context, String access, String fullname, String branch, String section, boolean isLoggedIn) {
SharedPreferences.Editor editor = context.getSharedPreferences("login.conf", Context.MODE_PRIVATE).edit();
editor.putString(ACCESS, access);
editor.putString(FULL_NAME, fullname);
editor.putString(BRANCH, branch);
editor.putString(SECTION, section);
editor.putBoolean(IS_LOGGED_IN, isLoggedIn);
return editor.commit();
}
// clear
public static void clearSP(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear().apply();
}
// get access info
public static String getAccess(Context context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getString(ACCESS, "");
}
// get branch info
public static String getFullName(Context context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getString(FULL_NAME, "");
}
// get fullname info
public static String getBranch(Context context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getString(BRANCH, "");
}
// get section info
public static String getSection(Context context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getString(SECTION, "");
}
// get isLoggedIn info
public static boolean getIsLoggedIn(Context context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getBoolean(ACCESS, false);
}
}
并在您的代码中。
// edited here
final String accessdb = SharedPreferenceUtils.getAccess(this);
final String fullnamedb = SharedPreferenceUtils.getFullName(this);
final String branchdb = SharedPreferenceUtils.getBranch(this);
final String sectiondb = SharedPreferenceUtils.getSection(this);
final HashMap data = new HashMap();
data.put("access", accessdb);
data.put("password", passworddb);
data.put("fullname", fullnamedb);
data.put("branch", branchdb);
data.put("section", sectiondb);
如果是记住
if(checkRememberMe){
SharedPreferenceUtils.createSP(LoginActivity.this,etLogin.getText().toString(),fullnamedb,branchdb,sectiondb,true);
}
关于android - 如何在 Android Studio 中使用 PostResponseAsyncTask 将 SharedPreferences 设置为下一个 Activity 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45810586/
我经常使用 SSMS 查询数据和构建数据集,我的 IT 部门负责数据库管理。 最近我发现了 Azure Data Studio,我喜欢: 智能感知 源代码控制(例如使用 Git) 来自社区的扩展 SQ
我想根据我使用的 visual studio 版本编译不同的东西,比如 #if VISUAL_STUDIO_VERSION > 2015 eventH?.Invoke(this, EventArgs.
我们的开发团队计划从 visual studio 2005 升级到 visual studio 2010 -- 跳过 visual studio 2008。 大部分项目是VB ASP.NET项目,使用
我的Visual Studio 2015无法构建2010平台工具集。它说: The build tools for Visual Studio 2010 (v100) cannot be found.
我目前正在使用 Visual Studio 2015 来编程 ASP.NET Core 应用程序。我对安装 Visual Studio 2017 有以下疑问: 什么被认为是最佳实践和/或最干净的方法?
尝试从扩展和更新获取 Visual Studio 扩展时,出现以下错误:- 向 visualstudiogallery.msdn.microsoft.com/Services/VStudio/Exte
这个问题在这里已经有了答案: Can Visual Studio Code and VS 2012 be installed on same computer? (1 个回答) 关闭去年。 在安装了
作为标准安装的一部分,Visual Studio Code 带有一个名为“Monokai Dimmed”的颜色主题。 有没有办法将它移植到 Visual Studio 2015?我检查了社区主题( h
我想开始编程 CUDA。 我已经安装了 Visual Studio 2010 Express。 我还安装了 nVidia nSight Visual Studio。 而且我具备所有常见的先决条件(Ne
Visual Studio Community Edition是否可以使用Visual Studio Online帐户上的存储库? 我一直为包含在Online帐户中的Visual Studio Onl
我有一个我一直在开发的应用程序,但在 android studio 上遇到了问题。当我点击“build->run”然后选择我的设备时,应用程序永远不会在我的手机上运行(并且自动出现的android-s
我正在使用Visual Studio2010。我面临的一个问题是,当我创建一个新的Web项目时,Visual Studio将创建该项目,并且不会在解决方案资源管理器中显示其解决方案。 另一件事是,我想
我通读了这里的许多帖子,却找不到一个有效的明确答案。因此,在花了一些时间使它生效之后,我认为应该发布它。 问题:发布配置文件将建立在服务器上,但不会发布。 解: 确保已安装Microsoft Wind
我正在尝试使用Visual Studio 2012构建针对.NET 3.5的C++ CLI应用程序。 通过安装Visual Studio 2008,并指定v90平台工具集,我已经在一台机器上进行了这项
我在 Microsoft Visual Studios 2013 中有一个项目,我想在 Microsoft Visual Studios 2010 中打开它。有什么简单的方法吗?还是我必须在2010年
我想知道,如果我发送一个解决方案文件夹(它是用 visual studio C# 编写的),您可以在 visual studio for mac 中打开解决方案吗? 在visual studio 20
有没有办法在 Visual Studio Code 和 Visual Studio 中设置相同的快捷方式(而不必每次都手动更改它们)? 例如,我在 Visual Studio Code 中经常使用 A
我刚开始了解 Visual Studio Code。我想知道,我可以将 Visual Studio 替换为所有 .NET 开发相关的工作吗? 我可以节省 Visual Studio 许可的成本吗? V
我安装了具有有效许可证(Visual Studio 订阅)的 Visual Studio 2019 企业版(VS 2019 16.1.4),它运行良好。 突然之间,当我尝试打开项目或项目中的任何文件时
我一直在使用 Compass 编译 Windows 环境中的 sass 文件,无论是在命令行上还是使用 Compass-app 来查看目录。 我刚刚开始使用 Visual Studio(专业版 201
我是一名优秀的程序员,十分优秀!