I am Able to Parse JSON Data.Exactly what is the issue is that ,i want to store the Student ID in the SharedPreference without suffix i.e means only the integer is to be stored? I have tried to split the JSON Value with REGEX but not able,since i have not used regex.How can this issue be solved??
{
"StdID":S001,
"NAME":"Kirsten Green",
"PHONENO":"095-517-0049",
"DOB":"2009-12-28T00:00:00",
"CLASS":9,
"GENDER":"M",
"ADDRESS":"8254 At Ave",
"NATIONALITY":"Belgium",
"ENROLLEDYEAR":"2016-04-21T00:00:00",
"Photo":null,
"Cat_ID":5,
"base64":null,
"studentDetails":{
"StdID":1,
"GUARDIAN_PHONE_NO":"002-283-4824",
"MOBILE_NO":"1-377-762-8548",
"First_NAME":"Maile",
"Last_Name":"Lancaster",
"Relation":"Father",
"DOB":"2017-02-22T00:00:00",
"Education":"Ph.D",
"Occupation":"Etiam ligula tortor,",
"Income":"20000-30000",
"Email":"urna@sed.ca",
"AddLine1":"Ap #416-4247 Sollicitudin Av.",
"AddLine2":"Ap #801-7380 Imperdiet Avenue",
"State":"ME",
"Country":"Israel"
},
"Marks":null,
"stdCategory":{
"Cat_ID":5,
"Category":"Normal"
}
}
My Java Class
public class Login extends AppCompatActivity implements View.OnClickListener {
EditText userName, Password;
Button login;
public static final String LOGIN_URL = "http://192.168.100.5:84/Token";
public static final String KEY_USERNAME = "UserName";
public static final String KEY_PASSWORD = "Password";
String username, password;
String accesstoken, tokentype, expiresin, masterid, name, access, issue, expires;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userName = (EditText) findViewById(R.id.login_name);
Password = (EditText) findViewById(R.id.login_password);
userName.setHint(Html.fromHtml("<font color='#008b8b' style='italic'>Username</font>"));
Password.setHint(Html.fromHtml("<font color='#008b8b'>Password</font>"));
login = (Button) findViewById(R.id.login);
login.setOnClickListener(this);
}
private void UserLogin() {
username = userName.getText().toString().trim();
password = Password.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
accesstoken = jsonObject.getString("access_token");
tokentype = jsonObject.getString("token_type");
expiresin = jsonObject.getString("expires_in");
username = jsonObject.getString("userName");
masterid = jsonObject.getString("MasterID");
//String[] parts = masterid.split("[0-9]");
//System.out.println(Arrays.toString(parts));
// parts = masterid.split("/[0-9]/g");
// parts = masterid.preg_replace('/[^0-9]/', '', $string);
name = jsonObject.getString("Name");
access = jsonObject.getString("Access");
issue = jsonObject.getString(".issued");
expires = jsonObject.getString(".expires");
SessionManagement session = new SessionManagement(Login.this);
session.createLoginSession(accesstoken, tokentype, expiresin, username, masterid, name, access, issue, expires);
// System.out.println(Arrays.toString(parts));
openProfile();
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
// params.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
return params;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> map = new HashMap<String, String>();
map.put(KEY_USERNAME, username);
map.put(KEY_PASSWORD, password);
map.put("grant_type", "password");
return map;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void openProfile() {
Intent intent = new Intent(this, Home.class);
intent.putExtra(KEY_USERNAME, username);
startActivity(intent);
}
@Override
public void onClick(View v) {
UserLogin();
}
}
can we neglect suffix used in JSON Data and store only integer?
The best way to pars a JSON is by using
Gson
. U can add it to your project in your gradle:Then you can easily create your model:
After that, just with
fromJson()
method, you can convert anyjson
to your model and retrieve yourstdId
from it.UPDATE:
If you want to get only the number from
S001
you can do it this way:after this, you have your number in
str
.