Example usage for com.squareup.okhttp Request.Builder url

List of usage examples for com.squareup.okhttp Request.Builder url

Introduction

In this page you can find the example usage for com.squareup.okhttp Request.Builder url.

Prototype

HttpUrl url

To view the source code for com.squareup.okhttp Request.Builder url.

Click Source Link

Usage

From source file:com.mummyding.app.leisure.database.cache.cache.DailyCache.java

License:Open Source License

private void loadOld(String date, final List<StoryBean> oldList, final List<StoryBean> newList) {
    Request.Builder builder = new Request.Builder();
    builder.url(DailyApi.daily_old_url + date);
    Request request = builder.build();
    HttpUtil.enqueue(request, new Callback() {
        @Override/*w w  w .j  a v  a2  s.c o m*/
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful() == false) {
                mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
                return;
            }
            String res = response.body().string();

            ArrayList<Integer> collectionIDs = new ArrayList<Integer>();
            for (int i = 0; i < oldList.size(); i++) {
                if (oldList.get(i).isCollected() == 1) {
                    collectionIDs.add(oldList.get(i).getId());
                }
            }

            // clear old data
            mList.clear();

            Gson gson = new Gson();
            StoryBean[] storyBeans = (gson.fromJson(res, DailyBean.class)).getStories();
            for (StoryBean storyBeen : storyBeans) {
                newList.add(storyBeen);
            }

            for (StoryBean storyBean : newList) {
                mList.add(storyBean);
            }

            // setCollection flag
            for (Integer id : collectionIDs) {
                for (int i = 0; i < mList.size(); i++) {
                    if (id.equals(mList.get(i).getId())) {
                        mList.get(i).setCollected(1);
                    }
                }
            }

            // notify
            mHandler.sendEmptyMessage(CONSTANT.ID_SUCCESS);
        }
    });
}

From source file:com.mummyding.app.leisure.database.cache.cache.NewsCache.java

License:Open Source License

@Override
public void load() {
    Request.Builder builder = new Request.Builder();
    builder.url(mUrl);
    Request request = builder.build();
    HttpUtil.enqueue(request, new Callback() {
        @Override//from  w  w w.  j a  va  2s.  c o  m
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
        }

        @Override
        public void onResponse(com.squareup.okhttp.Response response) throws IOException {
            if (response.isSuccessful() == false) {
                mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
                return;
            }
            InputStream is = new ByteArrayInputStream(
                    response.body().string().getBytes(Charset.forName("UTF-8")));
            try {
                ArrayList<String> collectionTitles = new ArrayList<String>();
                for (int i = 0; i < mList.size(); i++) {
                    if (mList.get(i).getIs_collected() == 1) {
                        collectionTitles.add(mList.get(i).getTitle());
                    }
                }

                mList.clear();
                mList.addAll(SAXNewsParse.parse(is));
                for (String title : collectionTitles) {
                    for (int i = 0; i < mList.size(); i++) {
                        if (title.equals(mList.get(i).getTitle())) {
                            mList.get(i).setIs_collected(1);
                        }
                    }
                }
                is.close();
                mHandler.sendEmptyMessage(CONSTANT.ID_SUCCESS);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

}

From source file:com.mummyding.app.leisure.database.cache.cache.ReadingCache.java

License:Open Source License

@Override
public void load() {
    Utils.DLog("from net reading size: " + mList.size());
    for (int i = 0; i < mUrls.length; i++) {
        String url = mUrls[i];/*  w  w  w .  j  a va2 s .  c o m*/
        Request.Builder builder = new Request.Builder();
        builder.url(url);
        Request request = builder.build();

        HttpUtil.enqueue(request, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
            }

            @Override
            public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                if (response.isSuccessful() == false) {
                    mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
                    return;
                }
                ArrayList<String> collectionTitles = new ArrayList<String>();
                for (int i = 0; i < mList.size(); i++) {
                    if (mList.get(i).getIs_collected() == 1) {
                        collectionTitles.add(mList.get(i).getTitle());
                    }
                }
                Gson gson = new Gson();
                BookBean[] bookBeans = gson.fromJson(response.body().string(), ReadingBean.class).getBooks();
                mList.clear();
                for (BookBean bookBean : bookBeans) {
                    mList.add(bookBean);
                }

                for (String title : collectionTitles) {
                    for (int i = 0; i < mList.size(); i++) {
                        if (title.equals(mList.get(i).getTitle())) {
                            mList.get(i).setIs_collected(1);
                        }
                    }
                }
                mHandler.sendEmptyMessage(CONSTANT.ID_SUCCESS);
            }
        });
    }

}

From source file:com.mummyding.app.leisure.database.cache.cache.ScienceCache.java

License:Open Source License

@Override
public void load() {
    Request.Builder builder = new Request.Builder();
    builder.url(mUrl);
    Request request = builder.build();
    HttpUtil.enqueue(request, new Callback() {
        @Override/* w w  w . ja v  a2 s  .  c  o  m*/
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
        }

        @Override
        public void onResponse(com.squareup.okhttp.Response response) throws IOException {
            if (response.isSuccessful() == false) {
                mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
                return;
            }

            ArrayList<String> collectionTitles = new ArrayList<String>();
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).getIs_collected() == 1) {
                    collectionTitles.add(mList.get(i).getTitle());
                }
            }

            mList.clear();
            Gson gson = new Gson();
            ArticleBean[] articleBeans = (gson.fromJson(response.body().string(), ScienceBean.class))
                    .getResult();
            for (ArticleBean articleBean : articleBeans) {
                mList.add(articleBean);
            }

            for (String title : collectionTitles) {
                for (int i = 0; i < mList.size(); i++) {
                    if (title.equals(mList.get(i).getTitle())) {
                        mList.get(i).setIs_collected(1);
                    }
                }
            }
            mHandler.sendEmptyMessage(CONSTANT.ID_SUCCESS);
        }
    });

}

From source file:com.mummyding.app.leisure.support.Utils.java

License:Open Source License

public static void getRawHtmlFromUrl(String url, Callback callback) {
    if (callback == null || TextUtils.isEmpty(url)) {
        return;/*from  ww  w . j a  v  a2 s . c o  m*/
    }
    Request.Builder builder = new Request.Builder();
    builder.url(url);
    Request request = builder.build();
    HttpUtil.enqueue(request, callback);
}

From source file:com.mummyding.app.leisure.ui.about.AboutFragment.java

License:Open Source License

@Override
public boolean onPreferenceClick(Preference preference) {

    if (mAppIntro == preference) {
        Intent toIntro = new Intent(getActivity(), AppIntroActivity.class);
        startActivity(toIntro);/*from ww  w .ja v a 2 s  .co  m*/
    } else if (mDemoVideo == preference) {
        Intent toVideo = new Intent(getActivity(), DemoVideoActivity.class);
        startActivity(toVideo);
    } else if (mCheckUpdate == preference) {
        progressBar.setVisibility(View.VISIBLE);

        Request.Builder builder = new Request.Builder();
        builder.url(CONSTANT.VERSION_URL);
        Request request = builder.build();
        HttpUtil.enqueue(request, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Snackbar.make(getView(), R.string.hint_fail_check_update, Snackbar.LENGTH_SHORT).show();
                handle.sendEmptyMessage(1);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String latestVersion = response.body().string();
                if (CONSTANT.CURRENT_VERSION.equals(latestVersion.trim())) {
                    Snackbar.make(getView(), getString(R.string.notify_current_is_latest),
                            Snackbar.LENGTH_SHORT).show();
                } else {
                    Snackbar.make(getView(), getString(R.string.notify_find_new_version) + latestVersion,
                            Snackbar.LENGTH_SHORT).show();
                }
                handle.sendEmptyMessage(1);
            }
        });

    } else if (mStarProject == preference) {
        Utils.copyToClipboard(getView(), getString(R.string.project_url));
    } else if (mShare == preference) {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.text_share_info));
        startActivity(Intent.createChooser(sharingIntent, getString(R.string.text_share_leisure)));

    } else if (mBlog == preference) {
        Utils.copyToClipboard(getView(), getString(R.string.author_blog));
    } else if (mGitHub == preference) {
        Utils.copyToClipboard(getView(), getString(R.string.author_github));
    } else if (mEmail == preference) {
        Utils.copyToClipboard(getView(), getString(R.string.author_email));
    }
    return false;
}

From source file:com.mummyding.app.leisure.ui.daily.DailyDetailsActivity.java

License:Open Source License

@Override
protected void onDataRefresh() {
    Request.Builder builder = new Request.Builder();
    builder.url(url);
    Request request = builder.build();
    HttpUtil.enqueue(request, new Callback() {
        @Override//from  w  ww. j a  v  a  2  s .  co  m
        public void onFailure(Request request, IOException e) {
            handler.sendEmptyMessage(CONSTANT.ID_FAILURE);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            String res = response.body().string();
            Utils.DLog(res);
            Gson gson = new Gson();
            dailyDetailsBean = gson.fromJson(res, DailyDetailsBean.class);
            cache.execSQL(DailyTable.updateBodyContent(DailyTable.NAME, dailyDetailsBean.getTitle(),
                    dailyDetailsBean.getBody()));
            cache.execSQL(DailyTable.updateBodyContent(DailyTable.COLLECTION_NAME, dailyDetailsBean.getTitle(),
                    dailyDetailsBean.getBody()));
            cache.execSQL(DailyTable.updateLargePic(DailyTable.NAME, dailyDetailsBean.getTitle(),
                    dailyDetailsBean.getImage()));
            cache.execSQL(DailyTable.updateLargePic(DailyTable.COLLECTION_NAME, dailyDetailsBean.getTitle(),
                    dailyDetailsBean.getImage()));

            imageUrl = dailyDetailsBean.getImage();
            body = dailyDetailsBean.getBody();

            handler.sendEmptyMessage(CONSTANT.ID_SUCCESS);
        }
    });
}

From source file:com.near.chimerarevo.fragments.PostsRecyclerFragment.java

License:Apache License

private void performTask() {
    shouldAddToStack = false;/*  w w  w .  j  av  a  2  s  .  c  o m*/
    shouldSmoothScroll = false;

    Bundle args = getArguments();

    Request.Builder rb = new Request.Builder().tag(FRAGMENT_TAG);

    if (args != null) {
        if (args.containsKey(Constants.KEY_CAT))
            rb.url(URLUtils.getUrl(1, args.getString(Constants.KEY_CAT)));
        else if (args.containsKey(Constants.KEY_TYPE))
            rb.url(URLUtils.getUrl(args.getString(Constants.KEY_TYPE)));
        else
            rb.url(URLUtils.getUrl());
    } else
        rb.url(URLUtils.getUrl());

    OkHttpUtils.getInstance().newCall(rb.build()).enqueue(new GetPostsCallback());
}

From source file:com.near.chimerarevo.fragments.PostsRecyclerFragment.java

License:Apache License

private void addPage() {
    page++;/*from  w ww. ja v a2 s  .  c  o  m*/
    counter = page;
    shouldAddToStack = true;
    shouldSmoothScroll = true;

    mLoading.setVisibility(View.VISIBLE);

    Bundle args = getArguments();

    Request.Builder rb = new Request.Builder().tag(FRAGMENT_TAG);

    if (args != null) {
        if (args.containsKey(Constants.KEY_CAT))
            rb.url(URLUtils.getUrl(page, args.getString(Constants.KEY_CAT)));
        else if (args.containsKey(Constants.KEY_TYPE))
            rb.url(URLUtils.getUrl(args.getString(Constants.KEY_TYPE), page));
        else
            rb.url(URLUtils.getUrl(page));
    } else
        rb.url(URLUtils.getUrl(page));

    OkHttpUtils.getInstance().newCall(rb.build()).enqueue(new GetPostsCallback());
}

From source file:com.near.chimerarevo.fragments.ProductsListFragment.java

License:Apache License

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    arrayList.clear();/*from w  w  w. j  av a  2s  .c om*/
    counter = 1;
    page = 1;

    Request.Builder rb = new Request.Builder().tag(FRAGMENT_TAG);

    if (mCat.getSelectedItemPosition() > 0 && mBrand.getSelectedItemPosition() > 0)
        rb.url(URLUtils.getProductsUrl(counter,
                String.valueOf(catIdList.get(mCat.getSelectedItemPosition() - 1)),
                brandsList.get(mBrand.getSelectedItemPosition())));
    else if (mCat.getSelectedItemPosition() > 0)
        rb.url(URLUtils.getProductsUrl(counter,
                String.valueOf(catIdList.get(mCat.getSelectedItemPosition() - 1)), ""));
    else if (mBrand.getSelectedItemPosition() > 0)
        rb.url(URLUtils.getProductsUrl(counter, "", brandsList.get(mBrand.getSelectedItemPosition())));
    else
        rb.url(URLUtils.getProductsUrl(counter, "", ""));

    isTaskRunning = true;
    OkHttpUtils.getInstance().newCall(rb.build()).enqueue(new GetProductsCallback());

    if (mLoadingToolbar != null)
        mLoadingToolbar.setVisibility(View.VISIBLE);
}