如何解决为什么改造从ASP Web服务获取数据太慢
我编写了一个android应用程序,但是它从服务器获取数据的速度太慢 我使用rxjava和改造来获取数据和Web服务已由asp.net编写 请告诉我该怎么办 我已经与邮递员检查过Web服务,它们很好而且很快 请查看我的代码,并告诉我它们有什么问题 这些是我的代码: 片段:
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(),false));
rv_homeFragment_topSelling.setAdapter(adapter);
}
@Override
public void showHitsProduct(StockListModel list) {
adapter = new HomeRecyclerAdapter(getViewContext(),list);
rv_homeFragment_hits.setLayoutManager(new linearlayoutmanager(getViewContext(),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);
}
}
演示者:
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());
}
});
}
}
这些是我的Okhttp客户端和拦截器:
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;
解决方法
示例用法(不是答案-仅用于调试)
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;
}
并将其添加到客户端
.addNetworkInterceptor(new LoginInterceptor())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。