1

I have an android app in which I need to get data from web api and save it into database.

I'm using retrofit to connect to web api and also I'm using active android.

So this is what I have created so far:

My table model "Partners", the data which I'm retrieving from web api and saving into this table Partners.

@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");}
} 

I have created ApiService

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

I have api helper

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;
    }
}

An this is my fragment where I'm trying to invoke method to get data from 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();
    }


    final Callback<List<Partners>> partnersCallback = new Callback<List<Partners>>() {
        @Override
        public void onResponse(Call<List<Partners>> call, Response<List<Partners>> response) {
            for(Partners partners) {
                partners.save();
            }
        }

        @Override
        public void onFailure(Call<List<Partners>> call, Throwable t) {

        }
    };
}

QUESTION: I would be grateful if someone could tell me what I did wrong here and guide me into right direction??

EDIT:

enter image description here

RubyDigger19
  • 835
  • 13
  • 38

3 Answers3

1

Your Call variable expects a Partners object, but the Callback sees a List<Partners> (that's what is wrong, unless you actually do expect a JSON list), meanwhile you have implements Callback<Partners>... So the types don't match.

Tip: To avoid confusion, rename your class to Partner since it's a single instance of one Partner

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

You must add Internet Permission on your manifest

<uses-permission android:name="android.permission.INTERNET"/>
Hoshouns
  • 2,420
  • 23
  • 24
0

I think you have three problems :

  1. The callback and call variable do not correspond
  2. If you see the documentation of retrofit2 you can see that the base url is concatenated with value of GET annotation so I think you url is https://here-is-part-of-link/partners.json/Partners when you call the ApiService
  3. When retrofit gets the answer if it's good it will use GSONConverter (use in your helper) which will need setter to initialize your object. So try to add getter and setter to your Parteners object
  • I made Callback and Call variables corresponded. Also changed link when I'm calling ApiService. And still not working. Still won't get all partners from web and also failed to save them into db (this part is also unclear to me). I'll post error log in question. – RubyDigger19 Jul 22 '16 at 15:14