代码之家  ›  专栏  ›  技术社区  ›  Thomas Athanasiou

android studio项目共享首选项

  •  0
  • Thomas Athanasiou  · 技术社区  · 7 年前

    SharedManager类:

    public class SharedPreManager {
    private static SharedPreManager mInstance;
    private RequestQueue mRequestQueue;
    private static Context mCtx;
    
    private static final String SHARED_PREF_NAME="mysharedpref12";
    private static final String KEY_USERNAME="name";
    private static final String KEY_ID="id";
    private static final String KEY_SURNAME="surname";
    private static final String KEY_AGE="age";
    private static final String KEY_IDENTITY="identity";
    
    private SharedPreManager(Context context) {
        mCtx = context;
    
    }
    
    public static synchronized SharedPreManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new SharedPreManager(context);
        }
        return mInstance;
    }
    
    public boolean userLogin(int id, String username, String surname, String identity) {
        SharedPreferences sharedPreferences= mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
    
        editor.putInt(KEY_ID, id);
        editor.putString(KEY_USERNAME, username);
        editor.putString(KEY_SURNAME, surname);
        editor.putString(KEY_IDENTITY, identity);
        editor.apply();
        return true;
    }
    

    登录类:

    public class Login extends AppCompatActivity {
    EditText UsernameET, PasswordET;
    CheckBox identity_doctor, identity_patient;
    private ProgressDialog progressDialog;
    private int identityId;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    
        if ( SharedPreManager.getInstance(this).isLoggedIn()) {
            finish();
            startActivity(new Intent(this, ProfileActivity.class));
            return;
        }
    
        UsernameET = (EditText)findViewById(R.id.editText_username);
        PasswordET = (EditText)findViewById(R.id.editText_password);
        identity_doctor = (CheckBox)findViewById(R.id.checkBox_doctor);
        identity_patient = (CheckBox)findViewById(R.id.checkBox_patient);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait...");
    }
    
    public void OnLogin(View view) {
        final String username = UsernameET.getText().toString().trim();
        final String password = PasswordET.getText().toString().trim();
    
        //progressDialog.show();
        StringRequest stringRequest = new StringRequest(
                Request.Method.POST,
                Constants.login_url,
                new Response.Listener<String>() {
    
                    @Override
                    public void onResponse(String response) {
                        progressDialog.dismiss();
                        try {
    
                            JSONObject object = new JSONObject(response);
    
                            if(!object.getBoolean("error")){
                                //that means the user successfully authenticated
    
                                if(SharedPreManager.getInstance(getApplicationContext()).userLogin(object.getInt("id"),
                                        object.getString("name"),
                                        object.getString("surname"),
                                        object.getString("identity"))){
                                    Toast.makeText(
                                            getApplicationContext(),
                                            "TRUE",
                                            Toast.LENGTH_LONG
                                    ).show();
                                }
                                SharedPreManager.getInstance(getApplicationContext())
                                        .userLogin(
                                                object.getInt("id"),
                                                object.getString("name"),
                                                object.getString("surname"),
                                                object.getString("identity")
                                        );
                                Toast.makeText(
                                        getApplicationContext(),
                                        "User successfully login",
                                        Toast.LENGTH_LONG
                                ).show();
                            }
                            else {
                                Toast.makeText(
                                        getApplicationContext(),
                                        object.getString("message"),
                                        Toast.LENGTH_LONG
                                ).show();
                            }
    
                            startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
                            finish();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        progressDialog.dismiss();
    
                        Toast.makeText(
                                getApplicationContext(),
                                error.getMessage(),
                                Toast.LENGTH_LONG
                        ).show();
                    }
                }
        ){
            @Override
            protected Map<String, String> getParams() {
                Map<String,String> params = new HashMap<>();
                params.put("username", username);
                params.put("password", password);
                return params;
            }
        };
    
        RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
    }
    

    这个问题似乎发生在SharedManager类的userLogin方法上。toast“User successfully login”没有出现。if语句为真。

    我不知道数据库是否有问题。如果需要请评论,我会编辑与数据库代码的职位。有什么想法吗?

    10-05 19:18:46.807 1520-1598/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: org.json.JSONException: No value for name 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at org.json.JSONObject.get(JSONObject.java:389) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at org.json.JSONObject.getString(JSONObject.java:550) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at com.example.thomas.medicalappointmentorganization.Login$1.onResponse(Login.java:73) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at com.example.thomas.medicalappointmentorganization.Login$1.onResponse(Login.java:60) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 10-05 19:18:46.819 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at android.os.Looper.loop(Looper.java:135) 10-05 19:18:46.820 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254) 10-05 19:18:46.820 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at java.lang.reflect.Method.invoke(Native Method) 10-05 19:18:46.820 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at java.lang.reflect.Method.invoke(Method.java:372) 10-05 19:18:46.820 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 10-05 19:18:46.820 2523-2523/com.example.thomas.medicalappointmentorganization W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

    1 回复  |  直到 7 年前
        1
  •  0
  •   Thomas Athanasiou    7 年前

    错误出现在php文件中。Json响应在php文件中没有包含“name”和“姓氏”。

    通过添加以下内容解决:

    $response['surname']=$user['surname']