如何使用翻新API从JSON响应中保存“共享首选项”中的“图像名称”?

时间:2019-03-07 13:13:09

标签: android sharedpreferences retrofit retrofit2 jsonresponse

我已经从相机捕获图像并通过Retrofit通过上载API上载。现在我已成功获得响应,例如:

{ "msg": "Image Upload Successful", "status": 1, "image": "115648975487_1551435779.png" }

我很困惑将“图像”名称保存为共享首选项,并在下一次API调用中将该名称作为参数发送。

这是我通过Retrofit API上传图像的代码:

public void imageUpload(final String imageEncoded){
    file = new File(imageEncoded);
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    builder.addFormDataPart("image", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));

    ApiCall.callPostRetrofit(context, AppConstants.uploadImage, builder, new ApiCallBackListner() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String msg = jsonObject.getString("msg");
                String status = jsonObject.getString("status");

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

        @Override
        public void onError(String error) {

        }
    });
}

1 个答案:

答案 0 :(得分:0)

此类包含用于保存或从共享首选项获取价值的 saveImage() getUser()方法。

public class SessionManager {
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    Context _context;
    int PRIVATE_MODE = 0;
    private static final String PREF_NAME = "Pref";
    public static  final String KEY_IMAGE="image";




    public SessionManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void saveImage(String imagename) {
        editor.putString(KEY_IMAGE,imagename);
        editor.commit();
    }

    public String getuser(String KeyName)
    {
        return pref.getString(KeyName,null);
    }




}

然后

public void imageUpload(final String imageEncoded){
    SessionManager session=new SessionManager(this);

    file = new File(imageEncoded);
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    builder.addFormDataPart("image", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));

    ApiCall.callPostRetrofit(context, AppConstants.uploadImage, builder, new ApiCallBackListner() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String msg = jsonObject.getString("msg");
                String status = jsonObject.getString("status");
                String image=jsonObject.getString("image");
                session.saveImage(image);// saving image name to shared preference.

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

        @Override
        public void onError(String error) {

        }
    });
}

要从sharedpreference获取图像名称,请仅在该活动或片段中写几行代码:-

SessionManager session=new SessionManager(context);
session.getuser(SessionManager.KEY_IMAGE );