如何将通过改造获得的数据存储到数据库中

时间:2016-07-23 11:51:34

标签: android retrofit2 activeandroid

我有一个Android应用程序,我通过改造从web api获取数据。

现在我需要将这些数据保存到我的表中。我正在使用活动的android。

我的桌面型号"合作伙伴",我从网络API中检索的数据,需要保存到此表合作伙伴。

@Table(name = "Partners")
public class Partners extends Model {
    @Expose
    @Column(name = "Name")
    public String name;
    public Partners() {}
    public List<Partners> getPartners() { return getMany(Partners.class, "Partners");}
} 

我创建了ApiService

public interface APIService {    
    @GET("Partners")
    Call<Partners> getPartners();
}

我有api助手

public class APIHelper {

    public static APIService apiService;

    public static APIService getApiService() {
        if (apiService == null) {
            Retrofit retrofit = new Retrofit.Builder().baseUrl("https://here-is-part-of-link/partners.json")
                    .addConverterFactory(GsonConverterFactory.create()).build();
            apiService = retrofit.create(APIService.class);
        }
        return apiService;
    }
}

这是我的片段,我试图调用方法从web api获取数据。

public class MainFragment extends Fragment implements Callback<Partners> {

    Button shippingFragment, deliveryFragment, measurementFragment, takeOversFragment;
    private Call<Partners> call;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment, parent, false);
        getActivity().setTitle("Title");
        shippingFragment = (Button) view.findViewById(R.id.shipping);
        deliveryFragment = (Button) view.findViewById(R.id.delivery);
        measurementFragment = (Button) view.findViewById(R.id.measurement);
        takeOversFragment = (Button) view.findViewById(R.id.take_overs);
        shippingFragment.setOnClickListener(btnFragmentListener);
        deliveryFragment.setOnClickListener(btnFragmentListener);
        measurementFragment.setOnClickListener(btnFragmentListener);
        takeOversFragment.setOnClickListener(btnFragmentListener);

        call = APIHelper.getApiService().getPartners();
        call.enqueue(this);

        return view;
    }

    Button.OnClickListener btnFragmentListener = (new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment = null;
            Intent intent = null;
            switch (v.getId()) {
                case R.id.shipping:
                    fragment = new ShippingFragment();
                    replaceFragment(fragment);
                    break;
                case R.id.delivery:
                    fragment = new DeliveryMain();
                    replaceFragment(fragment);
                    break;
                case R.id.measurement:
                    intent = new Intent(getActivity(), MeasurementMain.class);
                    startActivity(intent);
                    break;
                case R.id.take_overs:
                    intent = new Intent(getActivity(), AllLogs.class);
                    startActivity(intent);
                    break;
            }
        }
    });

    public void replaceFragment(android.support.v4.app.Fragment rFragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_holder, rFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onResponse(Call<Partners> call, Response<Partners> response) {

    }

    @Override
    public void onFailure(Call<Partners> call, Throwable t) {
        Log.e("Error!!!!!!!!", String.valueOf(call));
    }
}

现在我需要将数据保存到表格&#34;合作伙伴&#34;。我尝试在onResponse()方法中做到这一点,但我失败了。

问题:有人可以帮我保存从网络API到数据库的数据吗?

编辑: enter image description here

1 个答案:

答案 0 :(得分:0)

在onResponse

中这样做
 @Override
    public void onResponse(Call<Partners> call, Response<Partners> response) {

    List<Partners> partners = response.body().getPartners();
    //the response is stored in local object and now you can store it in sqlite db

    }