mardi 14 juin 2016

Storing data into Shared Preference

I'm current storing the username, password and ipAddress into shared preference when the user types in their details, however I want to also save imageID and imageName. The thing is imageID and imageName is not an input and is managed on the database according to the device(user). How can I save these two variables?

public static void login(final String username, final String password, final String ipAddress, final Callback callback) throws JSONException {

    OkHttpClient client = new OkHttpClient();

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("username", username);
    jsonObject.put("password", password);
    jsonObject.put("ip", ipAddress);

    RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
    Request request = new Request.Builder()
            .url("http://"+ipAddress+"/api/v0/login")
            .post(body)
            .build();

    client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {


        // produces exception if db connection request has fail
        @Override
        public void onFailure(Request request, IOException e) {
            callback.onLoginFailure(e);
        }

        // checks is db request passed or fail
        @Override public void onResponse(Response response){
            if (!response.isSuccessful()) {
                callback.onLoginFailure(new IOException("Unexpected code " + response));
                return;
            }

            String jsonAsString = null;

            try {
                jsonAsString = response.body().string();

                JSONObject json = new JSONObject(jsonAsString);

                if (json.getString("status").equals("ok")){

                    Device device = new Device();
                    device.locationID = json.getInt("location_id");
                    //device.imageID = json.getInt("imageID");
                    //device.imageName = json.getString("imageName");

                    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.getActivity());

                    sharedPreferences.edit()
                            .putString("ip", ipAddress)
                            .putString("username", username)
                            .putString("password", password)
                            //.putString("imageID", imageID)
                            //putString("imageName", imageName)
                            .commit();

                    device.id = json.getInt("id");

                    Device.instance = device;

                    callback.onLoginSuccess(device);
                } else {
                    throw new JSONException("Invalid service response");
                }

            } catch (Exception e) {
                e.printStackTrace();
                callback.onLoginFailure(e);
            }
        }
    });
}

Aucun commentaire:

Enregistrer un commentaire