Reputation: 295
I am a newbie to android. I am trying to use Volley for posting my form data to server.I am able to successfully post the data, and I am getting respons like this
{"status":"success","username":"User Vishnu","userid":"124","role":"UU"}
I need to store the values of this response to sharedpreference. How can I achieve the same?
Thanks in advance. I have the following code written in my activity
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, loginURL,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
pDialog.dismiss();
JSONObject josnOBJ = new JSONObject(response);
JSONArray jArray = josnOBJ.getJSONArray();
JSONObject jsonData = null;
String title = null;
for (int i=0; i < jArray.length(); i++)
{
try {
jsonData = jArray.getJSONObject(i);
title= jsonData .optString("role");
} catch (JSONException e) {
// Oops
}
}
String bla= response.toString();
Log.i("Responses",bla);
Toast.makeText(LoginActivity.this,bla,Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, DrawerSelectorActvity.class);
startActivity(intent);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}).
PS: There are errors in the code.This is the code I have tried so far.
Upvotes: 0
Views: 1914
Reputation: 3319
Try this
Add code in volley onResponse.
JSONObject josnOBJ = new JSONObject(response);
String username= jsonOBJ.getString("username");
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("username", username);
editor.commit(); // or editor.apply();
Upvotes: 2