如何保存使用翻新收到的响应?

时间:2018-09-28 04:26:58

标签: android retrofit2

我知道这是基本问题,但是我是新的android。我已经使用Retrofit从URL获得响应,所以我创建了POJO类和接口。
  因此,我从URL得到响应,但不了解如何保存响应,我可以在其他活动或片段中使用该响应,或者将值分配给edittext,请帮助我

{
    "statusCode": "200",
    "statusMessage": "SUCCESS",
    "meetmeConfig": {
        "id": "bb52dc0f-29d0-4079-99c7-a07c8045a829",
        "moduleName": "MeetMe",
        "createdDate": 1523962430721,
        "configContent": {
            "trackingOptions": [
                {
                    "optionName": "Before the meet",
                    "isTimeRequired": true,
                    "trackingTime": [
                        5,
                        10,
                        15
                    ],
                    "isDeleted": false
                },
                {
                    "optionName": "After the meet",
                    "isTimeRequired": true,
                    "trackingTime": [
                        5,
                        10,
                        15
                    ],
                    "isDeleted": false
                },
                {
                    "optionName": "At the start",
                    "isTimeRequired": false,
                    "trackingTime": [],
                    "isDeleted": false
                },
                {
                    "optionName": "Never",
                    "isTimeRequired": false,
                    "trackingTime": [],
                    "isDeleted": false
                }
            ],
            "numberOfParticipants": 8,
            "mapResetTimeInterval": 30,
            "meetingTrackableTime": 3600,
            "addressTypes": [
                "Home",
                "Work"
            ],
            "transportModes": [
                "Walking",
                "Driving"
            ],
            "gender": [
                "Male",
                "Female",
                "Trans-Female",
                "Bi-Gender",
                "Non-Binary",
                "Gender nonconfirming",
                "Undisclosed",
                "Rather not say"
            ],
            "toastDelayTimeForPulse": 3,
            "syncToastMaxTimeInterval": 300,
            "syncToastThirdTimeInterval": 180,
            "firstTimeInterval": 1,
            "secondTimeInterval": 2,
            "meetmeSearchContactTimeInterval": 2,
            "signupToastDelayTime": 4,
            "signupToastdelatimebysix": 6,
            "signupToastDelayMedium": 5,
            "profileToastDelayTime": 4,
            "profileToastDelatimebysix": 6,
            "languages": [
                "English",
                "Spanish",
                "Marathi",
                "Hindi",
                "Bengali",
                "French",
                "Arabic",
                "German",
                "Italian",
                "Dutch",
                "Japanese",
                "Russia",
                "Korean"
            ]
        }
    }
}

3 个答案:

答案 0 :(得分:0)

尝试Google的Room Persistence Library // https://codelabs.developers.google.com/codelabs/android-room-with-a-view/

Call<YourPojo> apiCall = yourinterface.method(
    Param1,
    Param2
);

 apiCall.enqueue(new Callback<YourPojo>() {
    @Override
    public void onResponse(Call<YourPojo> yourPojo, Response<BPResponse> response) {
        //here you can save response to Database/SharedPreferences etc

    }

    @Override
    public void onFailure(Call<YourPojo> yourPojo, Throwable t) {

    }
});

答案 1 :(得分:0)

我假设您对保存json的响应感到困惑。所以我给你一个简单的例子

例如:

Json的回复:

{
"STATUS": "FAILED",
"MESSAGE": "PLEASE LOGIN"
}

Pojo课堂:

@SerializedName("STATUS")
@Expose
private String success; //object

@SerializedName("MESSAGE")
@Expose
private String message;

public String getSuccess() {
    return success;
}

public void setSuccess(String success) {
    this.success = success;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

如何调用它或保存响应:

Call<yourpojo> call = youinterface.getResult();
    call.enqueue(new Callback<yourpojo>() {
        @Override
        public void onResponse(Call<yourpojo> call, Response<yourpojo> response) {
            try {
                if (response.isSuccessful()) {
                    String status = response.body().getStatus();
        String message = response.body().getMessage();
                } else {
                    try {
                        Converter<ResponseBody, ApiError> converter = ApiHandler.getApi(getActivity()).responseBodyConverter(ApiError.class, (java.lang.annotation.Annotation[]) new Annotation[0]);
                        ApiError error = converter.convert(response.errorBody());
                        Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Toast.makeText(getActivity(), response.errorBody().string(), Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (Exception e) {
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFailure(Call<yourpojo> call, Throwable t) {
            Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

我希望我的榜样能对您有所帮助!有任何疑问请与我联系

答案 2 :(得分:0)

这取决于您保存并共享到活动或片段的含义:

  1. 如果要永久保存数据,建议查看Room Persistence。

您可以查看此链接,它说明了使用翻新和房间的一种很好的方法:

https://proandroiddev.com/the-missing-google-sample-of-android-architecture-components-guide-c7d6e7306b8f

您可以在以下链接中找到一个简单的Room教程: https://www.pluralsight.com/guides/making-a-notes-app-using-room-database

  1. 如果只想将数据传递给活动,则可以将结果保存在变量中,然后将它们传递给Intent。

  2. 如果要保存小变量并且不想创建数据库,则可以使用SharedPreferences保存数据。