我希望在登录期间将以下jsonArray存储在会话中,并在以后需要时在另一个活动中逐个访问这些元素。我完全糊涂了。登录成功后,我收到以下数据。我该怎么做?这是我的登录页面
public class Login extends AppCompatActivity {
String mobile, password;
TextView forgot_password;
EditText mobile_num,pass;
Button log_btn;
AlertDialog.Builder builder;
private Session session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Login.this.setTitle("Vendor Screen");
builder = new AlertDialog.Builder(Login.this);
forgot_password = findViewById(R.id.forgot_pwd);
log_btn = findViewById(R.id.login_btn);
mobile_num = findViewById(R.id.mobile);
pass = findViewById(R.id.password);
forgot_password.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Login.this,Forgot_password.class);
startActivity(i);
}
});
//Login Page
log_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ConnectivityManager ConnectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = ConnectionManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected() == true) {
mobile = mobile_num.getText().toString();
password = pass.getText().toString();
String url = "https://vendorapp.web4rest.com/vendorController/auth?username=" + mobile + "&password=" + password;
if (mobile.equals("") || password.equals("")) {
builder.setTitle("Something went wrong!!");
displayAlert("Enter valid mobile number and password");
}else{
final ProgressDialog progressDialog = new ProgressDialog(Login.this);
progressDialog.setMessage("Please Wait...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
progressDialog.dismiss();
JSONObject jobject = new JSONObject(response);
if (Integer.parseInt(jobject.getString("status")) == 200) {
Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_LONG).show();
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
finish();
} else {
Toast.makeText(getApplicationContext(), "Login Unsuccessfull. Check mobile number/password", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
progressDialog.dismiss();
Log.e("Error", "Login Failed" + e.toString());
e.printStackTrace();
Toast.makeText(Login.this, "Error", Toast.LENGTH_SHORT);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(Login.this, "Error", Toast.LENGTH_SHORT);
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parmams = new HashMap<String, String>();
parmams.put("username", mobile);
parmams.put("password", password);
return parmams;
}
};
MySingleton.getInstance(Login.this).addToRequestque(stringRequest);
}
}else {
Toast.makeText(Login.this, "Network Unavailable", Toast.LENGTH_LONG).show();
}
}
});
}
public void displayAlert(String message){
builder.setMessage(message);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
mobile_num.setText("");
pass.setText("");
}
});
AlertDialog alertDialog =builder.create();
alertDialog.show();
}
{ &#34;状态&#34;:200, &#34; userdata&#34;:{ &#34; ID&#34;:&#34; 2&#34;, &#34; FIRM_NAME&#34;:&#34;公司名称&#34;, &#34; CONTACT_NO&#34;:&#34; 7458236965&#34;, &#34; FIRM_EMAIL&#34;:&#34; comp@gmail.com", &#34; STATE&#34;:&#34; Karnataka&#34;, &#34; CITY&#34;:&#34; Anantapur&#34;, &#34; ADDRESS_LINE1&#34;:&#34; dgsdfgsdf&#34;, &#34; ADDRESS_LINE2&#34;:null, &#34; TYPE&#34;:&#34; UNREG&#34;, &#34; CATEGORY_ID&#34;:&#34; 1&#34;, &#34; REG_NO&#34;:null, &#34; GST_NO&#34;:null, &#34; ECC_CODE&#34;:&#34; 744411228528&#34;, &#34; PAN_NO&#34;:&#34; 7455852125&#34;, &#34; FAX_NO&#34;:&#34; 741258963&#34;, &#34; PIN_CODE&#34;:&#34; 590006&#34;, &#34; CONTACT_PERSON&#34;:&#34; Ashwini&#34;, &#34; PERSON_EMAIL&#34;:&#34; koustubhc@web4rest.com", &#34; PERSON_MOBILE&#34;:&#34; 7798671682&#34;, &#34; BANK&#34;:&#34; Allahabad Bank&#34;, &#34; IS_DELETE&#34;:&#34; 0&#34;, &#34; IFSC_CODE&#34;:&#34; ALLB00006&#34;, &#34; BRANCH&#34;:&#34; Allahbad&#34;, &#34; ACCOUNT_NO&#34;:&#34; 033252122522&#34;, &#34; INITIAL_PASS&#34;:&#34; PUTLANvq&#34;, &#34; PASSWORD&#34;:&#34; fd2811b88d59df925f1a265911ffc539&#34;, &#34; OTP&#34;:&#34; 6979&#34;, &#34; CREATED_AT&#34;:&#34; 2018-05-31 13:02:40&#34;, &#34; UPDATED_AT&#34;:&#34; 2018-05-14 08:59:10&#34;, &#34; status&#34;:&#34;登录&#34; } }
答案 0 :(得分:1)
您可以遵循以下基本想法:
if (Integer.parseInt(jobject.getString("status")) == 200 {
...
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
var userData = jobject.getObject("userdata");
editor.putInt("ID", userData.getInt("ID"));
editor.putString("FIRM_NAME", userData.getString("FIRM_NAME"));
// etc...
editor.commit();
...
}
然后在你的其他活动中:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
var id = preferences.getInt("ID", -1);
var firmName = preferences.getString("FIRM_NAME", null);
如果未找到偏好设置,则上面的-1
和null
将是给定的值。