Kelen Luana Zimermann Kelen Luana Zimermann - 2 months ago 10
JSON Question

Correct way of getting a Json position

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

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


Ok. Now My android code.

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

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

} catch (JSONException e) {
e.printStackTrace();
}


}


This code Toast the following message. "Bem vindo lu@lu.com"

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

Email = "lu@lu.com"

Endereco = "Rua Pedro..."

Name = "Lu Zimermann"

Soo I can use it later on the app.

Thanks.

Answer

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

SharedPreferenceCustom

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));
        editor.apply();

    }

    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) {
    e.printStackTrace();
 }

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 :) :)

Comments