I have written an android app but it fetches data from server too slow I used rxjava and retrofit for fetching data and web services have been written by asp.net please tell me what should I do I have checked web services with postman and they are fine and fast please review my codes and tell me what's wrong with them these are my codes: Fragment:
public class HomeFragment extends BaseFragment implements HomeContract.View {
private HomeContract.Presenter presenter;
private SliderView slider;
private TextView txt_homeFragment_showAllNew,
txt_homeFragment_showAllTopSelling,
txt_homeFragment_showAllHits;
private RecyclerView rv_homeFragment_newProduct,
rv_homeFragment_topSelling,
rv_homeFragment_hits,
rv_homeFragment_specialOffer;
private Button btn_homeFragment_showAllSpecial;
private HomeRecyclerAdapter adapter;
private Fragment fragment;
private FragmentTransaction transaction;
private Bundle bundle;
@SuppressLint("StaticFieldLeak")
private Dialog myDialog;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String pls = getResources().getString(R.string.please_wait);
String loading = getResources().getString(R.string.loading);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter = new HomePresenter(new TCommerceRepository(),getViewContext());
if(MainFragment.checkInternet) {
presenter.getNewProduct();
presenter.getHitsProduct();
presenter.getSlider();
}
}
@Override
public int getLayout() {
return R.layout.fragment_home;
}
@Override
public void setupViews() {
myDialog = new Dialog(getContext());
myDialog.setContentView(R.layout.dialog_alert);
TextView txtTitle = myDialog.findViewById(R.id.txt_alertDialog_title);
TextView txtMessage = myDialog.findViewById(R.id.txt_alertDialog_message);
String pls = getResources().getString(R.string.please_wait);
String loading = getResources().getString(R.string.loading);
txtTitle.setText(loading);
txtMessage.setText(pls);
myDialog.getWindow().setLayout(1000,500);
slider = rootView.findViewById(R.id.slider_homeFragment_slideShow);
txt_homeFragment_showAllNew = rootView.findViewById(R.id.txt_homeFragment_showAllNew);
txt_homeFragment_showAllTopSelling = rootView.findViewById(R.id.txt_homeFragment_showAllTopSelling);
txt_homeFragment_showAllHits = rootView.findViewById(R.id.txt_homeFragment_showAllHits);
rv_homeFragment_newProduct = rootView.findViewById(R.id.rv_homeFragment_newProduct);
rv_homeFragment_topSelling = rootView.findViewById(R.id.rv_homeFragment_topSelling);
rv_homeFragment_hits = rootView.findViewById(R.id.rv_homeFragment_hits);
rv_homeFragment_specialOffer = rootView.findViewById(R.id.rv_homeFragment_specialOffer);
btn_homeFragment_showAllSpecial = rootView.findViewById(R.id.btn_homeFragment_showAllSpecial);
txt_homeFragment_showAllNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
bundle.putInt("flag",1);
fragment = new StockListFragment();
fragment.setArguments(bundle);
transaction = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frm_mainFragment_mainLayout, fragment);
transaction.commit();
}
});
txt_homeFragment_showAllTopSelling.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
bundle.putInt("flag",2);
fragment = new StockListFragment();
fragment.setArguments(bundle);
transaction = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frm_mainFragment_mainLayout, fragment);
transaction.commit();
}
});
txt_homeFragment_showAllHits.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
bundle.putInt("flag",3);
fragment = new StockListFragment();
fragment.setArguments(bundle);
transaction = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frm_mainFragment_mainLayout, fragment);
transaction.commit();
}
});
}
@Override
public Context getViewContext() {
return getContext();
}
@Override
public void showMessage(String msg) {
Log.i("LOG::::" , msg);
}
@Override
public void onStart() {
super.onStart();
presenter.attachView(this);
}
@Override
public void onStop() {
super.onStop();
presenter.detachView();
}
@Override
public void showNewProduct(StockListModel list) {
adapter = new HomeRecyclerAdapter(getViewContext() , list);
rv_homeFragment_newProduct.setLayoutManager(new LinearLayoutManager(getViewContext() , RecyclerView.HORIZONTAL , false));
rv_homeFragment_newProduct.setAdapter(adapter);
}
@Override
public void showTopSellProduct(StockListModel list) {
adapter = new HomeRecyclerAdapter(getViewContext() , list);
rv_homeFragment_topSelling.setLayoutManager(new LinearLayoutManager(getViewContext() , RecyclerView.HORIZONTAL , false));
rv_homeFragment_topSelling.setAdapter(adapter);
}
@Override
public void showHitsProduct(StockListModel list) {
adapter = new HomeRecyclerAdapter(getViewContext() , list);
rv_homeFragment_hits.setLayoutManager(new LinearLayoutManager(getViewContext() , RecyclerView.HORIZONTAL , false));
rv_homeFragment_hits.setAdapter(adapter);
if (myDialog.isShowing()){
myDialog.dismiss();
}
}
@Override
public void showSlider(ItemImageSlider itemImageSlider) {
if ((myDialog.isShowing())){
myDialog.dismiss();
} else {
myDialog.show();
}
TimerTask task = slider.getTimerTask();
Timer timer = new Timer();
timer.schedule(task,5000,2000);
ArrayList<String> urls = new ArrayList<>();
for (int i = 0; i < itemImageSlider.getItems().size(); i++) {
urls.add(itemImageSlider.getItems().get(i).getIcon());
}
slider.setUrls(urls);
}
}
Presenter:
public class HomePresenter implements HomeContract.Presenter {
private HomeContract.View view;
private TCommerceDataSource tCommerceDataSource;
private CompositeDisposable compositeDisposable = new CompositeDisposable();
private Context context;
HomePresenter (TCommerceDataSource tCommerceDataSource,Context context){
this.tCommerceDataSource = tCommerceDataSource;
this.context = context;
}
@Override
public void attachView(HomeContract.View view) {
this.view = view;
}
@Override
public void detachView() {
this.view = null;
if (compositeDisposable != null){
compositeDisposable.clear();
}
}
@Override
public void getNewProduct() {
tCommerceDataSource.getListStockNew().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<StockListModel>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(StockListModel productList) {
view.showNewProduct(productList);
view.showTopSellProduct(productList);
}
@Override
public void onError(Throwable e) {
view.showMessage(e.toString());
}
});
}
@Override
public void getHitsProduct() {
tCommerceDataSource.getListStockBestView().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<StockListModel>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(StockListModel stockListModel) {
view.showHitsProduct(stockListModel);
}
@Override
public void onError(Throwable e) {
view.showMessage(e.toString());
}
});
}
@Override
public void getSlider() {
tCommerceDataSource.getSlideImage().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<ItemImageSlider>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(ItemImageSlider itemImageSlider) {
view.showSlider(itemImageSlider);
}
@Override
public void onError(Throwable e) {
view.showMessage(e.toString());
}
});
}
}
and these are my Okhttp client and interceptor:
OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(6 , TimeUnit.MINUTES)
.readTimeout(6 , TimeUnit.MINUTES)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.build();
return chain.proceed(request);
}
}).build();
return httpClient;
How Should I use Interceptor? What should I change?
A Sample how to use, (not an answer - just for debugging)
public class LoginInterceptor extends Interceptor {
private static final String TAG = LoginInterceptor.class.getSimpleName() + " : ";
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Log.i(TAG, String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Log.d(TAG, String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
and add this to client
.addNetworkInterceptor(new LoginInterceptor())