Kelen Luana Zimermann Kelen Luana Zimermann - 1 month ago 6
JSON Question

Correct way of getting a Json position

After the login I get this response from the webserver..

{"success":" Bem vindo"}{"nomeusuario":"Lu Zimermann"}{"enderecousuario":"Rua Pedro Alves 270. Centro. Casa."}{"telefoneusuario":"(42) 3623-8052"}

Ok. Now My android code.

loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
//startActivity(new Intent(getApplicationContext(),Restaurantes.class));
}else {
Toast.makeText(getApplicationContext(), jsonObject.getString("error"), Toast.LENGTH_SHORT).show();

} catch (JSONException e) {


This code Toast the following message. "Bem vindo"

What I want is:
How can I get the other infos and pass it to a String.

Email = ""

Endereco = "Rua Pedro..."

Name = "Lu Zimermann"

Soo I can use it later on the app.



Now the response is the right way you can do this to save data.. first wirte a custion class for Preferennce


public class SharedPreferenceCustom {
    private Context mContext;
    private String defValue = "";

    public SharedPreferenceCustom(Context context) {
        this.mContext = context;

    public void setSharedPref(String inputKey, Object inputValue) {
        final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(inputKey, String.valueOf(inputValue));


    public String getSharedPref(String inputKey) {
        SharedPreferences sharedPreferences =  PreferenceManager.getDefaultSharedPreferences(mContext);
        return sharedPreferences.getString(inputKey, defValue);

Now for saving the data, in the activity

 try {
    JSONObject response = new JSONObject(new String(response_from_server))
    SharedPreferenceCustom sp = new SharedPreferenceCustom(Activity.this); 
    sp.setSharedPref("key_1", response.getString("nomeusuario"));   // use key as per needed   
    sp.setSharedPref("key_2", response.getString("enderecousuario"));
    sp.setSharedPref("key_3", response.getString("telefoneusuario"));
    sp.setSharedPref("key_4", response.getString("success"));  
 } catch (JSONException e) {

And for getting the data call sp.getSharedPref("key"); pass the key for getting the corresponding data

EDIT: you can also write individual function in SharedPreferenceCustion to store different data type, or you can use just this, But it might create some conflicts for certain data types,

Hope this helps :) :)