com.lzy.okgo.OkGo Java Examples
The following examples show how to use
com.lzy.okgo.OkGo.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DownloadListActivity.java From okhttp-OkGo with Apache License 2.0 | 6 votes |
@OnClick(R.id.startAll) public void startAll(View view) { for (ApkModel apk : apks) { //这里只是演示,表示请求可以传参,怎么传都行,和okgo使用方法一样 GetRequest<File> request = OkGo.<File>get(apk.url)// .headers("aaa", "111")// .params("bbb", "222"); //这里第一个参数是tag,代表下载任务的唯一标识,传任意字符串都行,需要保证唯一,我这里用url作为了tag OkDownload.request(apk.url, request)// .priority(apk.priority)// .extra1(apk)// .save()// .register(new LogDownloadListener())// .start(); adapter.notifyDataSetChanged(); } }
Example #2
Source File: LivingFragment.java From MyHearts with Apache License 2.0 | 6 votes |
/** * 请求banner图片 */ private void initBannerData() { OkGo.get(HttpUrlPaths.LIVING_STREAMING_BANNER_URL) .params("type", "live") .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<LivingBannerBean>() { }.getType(); LivingBannerBean bean = new Gson().fromJson(s, type); if (bean.getErrorCode() == 0 && bean.getErrorStr().equals("success") && bean.getResultCount() > 0) { beanResults = bean.getResults(); updateBannerUI(beanResults); } }, throwable -> { }); }
Example #3
Source File: DownloadListActivity.java From okhttp-OkGo with Apache License 2.0 | 6 votes |
@OnClick(R.id.download) public void download() { //这里只是演示,表示请求可以传参,怎么传都行,和okgo使用方法一样 GetRequest<File> request = OkGo.<File>get(apk.url)// .headers("aaa", "111")// .params("bbb", "222"); //这里第一个参数是tag,代表下载任务的唯一标识,传任意字符串都行,需要保证唯一,我这里用url作为了tag OkDownload.request(apk.url, request)// .priority(apk.priority)// .extra1(apk)// .save()// .register(new LogDownloadListener())// .start(); adapter.notifyDataSetChanged(); }
Example #4
Source File: HomeFragment.java From MyHearts with Apache License 2.0 | 6 votes |
/** * 请求首页下面展示的图片的第一个banner */ private void initRecommentOne() { OkGo.post(HttpUrlPaths.HOME_RECOMMENT_ONE_BANNER) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<HomeBannerOne>() { }.getType(); HomeBannerOne homeBannerOne = new Gson().fromJson(s, type); if (homeBannerOne.getErrorStr().equals("success") && homeBannerOne.getErrorCode() == 0) { Glide.with(getContext()) .load(homeBannerOne.getResults().getBanner()) .asBitmap() .into(mRecommentImgOne); } }, throwable -> { }); }
Example #5
Source File: Progress.java From okhttp-OkGo with Apache License 2.0 | 6 votes |
public static Progress changeProgress(final Progress progress, long writeSize, long totalSize, final Action action) { progress.totalSize = totalSize; progress.currentSize += writeSize; progress.tempSize += writeSize; long currentTime = SystemClock.elapsedRealtime(); boolean isNotify = (currentTime - progress.lastRefreshTime) >= OkGo.REFRESH_TIME; if (isNotify || progress.currentSize == totalSize) { long diffTime = currentTime - progress.lastRefreshTime; if (diffTime == 0) diffTime = 1; progress.fraction = progress.currentSize * 1.0f / totalSize; progress.speed = progress.bufferSpeed(progress.tempSize * 1000 / diffTime); progress.lastRefreshTime = currentTime; progress.tempSize = 0; if (action != null) { action.call(progress); } } return progress; }
Example #6
Source File: HomeFragment.java From MyHearts with Apache License 2.0 | 6 votes |
/** * 请求推荐咨询师数据 */ private void initAdvisoryData() { OkGo.post(HttpUrlPaths.HOME_RECOMMENT) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<AdvisoryBean>() { }.getType(); AdvisoryBean bean = new Gson().fromJson(s, type); if (bean != null && bean.getErrorCode() == 0 && bean.getErrorStr().equals("success")) { mAdvisoryBean = bean.getResults(); if (mAdvisoryBean != null && mAdvisoryBean.size() > 0) { initAdvisory(mAdvisoryBean); } } }, throwable -> { }); }
Example #7
Source File: UploadAdapter.java From okhttp-OkGo with Apache License 2.0 | 6 votes |
public List<UploadTask<?>> updateData(List<ImageItem> images) { this.type = -1; this.images = images; values = new ArrayList<>(); if (images != null) { Random random = new Random(); for (int i = 0; i < images.size(); i++) { ImageItem imageItem = images.get(i); //这里是演示可以传递任何数据 PostRequest<String> postRequest = OkGo.<String>post(Urls.URL_FORM_UPLOAD)// .headers("aaa", "111")// .params("bbb", "222")// .params("fileKey" + i, new File(imageItem.path))// .converter(new StringConvert()); UploadTask<String> task = OkUpload.request(imageItem.path, postRequest)// .priority(random.nextInt(100))// .extra1(imageItem)// .save(); values.add(task); } } notifyDataSetChanged(); return values; }
Example #8
Source File: Request.java From BaseProject with Apache License 2.0 | 6 votes |
public Request(String url) { this.url = url; baseUrl = url; OkGo go = OkGo.getInstance(); //默认添加 Accept-Language String acceptLanguage = HttpHeaders.getAcceptLanguage(); if (!TextUtils.isEmpty(acceptLanguage)) headers(HttpHeaders.HEAD_KEY_ACCEPT_LANGUAGE, acceptLanguage); //默认添加 User-Agent String userAgent = HttpHeaders.getUserAgent(); if (!TextUtils.isEmpty(userAgent)) headers(HttpHeaders.HEAD_KEY_USER_AGENT, userAgent); //添加公共请求参数 if (go.getCommonParams() != null) params(go.getCommonParams()); if (go.getCommonHeaders() != null) headers(go.getCommonHeaders()); //添加缓存模式 retryCount = go.getRetryCount(); cacheMode = go.getCacheMode(); cacheTime = go.getCacheTime(); }
Example #9
Source File: HotFragment.java From MyHearts with Apache License 2.0 | 6 votes |
/** * 请求数据 */ private void initData() { OkGo.post(HttpUrlPaths.THOUGHTS_URL) .params("userid",54442) .params("labelid",catgId) .params("type",1) .params("page",page) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(()->{}) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s->{ Type type = new TypeToken<ThoughtsBean>(){}.getType(); ThoughtsBean bean = new Gson().fromJson(s,type); if (bean.getErrorCode()==0 &&bean.getResultCount()>0 &&bean.getErrorStr().equals("success")){ mThroughtDatas = bean.getResults(); mThroughtAdapter.setResultsBeen(mThroughtDatas); /// mRefreshLayout.setClickable(true); // mRefreshLayout.setLoadMore(true); } },throwable -> {}); }
Example #10
Source File: OrationActivity.java From MyHearts with Apache License 2.0 | 6 votes |
@Override public void initData() { super.initData(); OkGo.post(HttpUrlPaths.SCAN_MORE) .params("userid", "54442") .params("page", page + "") .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<HomeNewsBean>() { }.getType(); HomeNewsBean bean = new Gson().fromJson(s, type); if (bean.getErrorCode() == 0 && bean.getErrorStr().equals("success") && bean.getResults().size() > 0) { mOrationDatas = bean.getResults(); mOrationAdapter.addData(mOrationDatas); } }, throwable -> { }); }
Example #11
Source File: CategoryFragment.java From MyHearts with Apache License 2.0 | 6 votes |
/** * 下拉刷新数据 * * @param url url */ private void requestData(String url) { limit = lordMoreNum; offset = 0; if (mIndex == 0) { url = HttpUrlPaths.getDouyuLiveChannel(limit, offset); } else { url = HttpUrlPaths.getDouyuSubChannelBaseTag(mIndex, limit, offset); } OkGo.get(url) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<RoomInfo>() { }.getType(); RoomInfo roomInfo = new Gson().fromJson(s, type); if (roomInfo.getData().size() > 0) { mDataEntities.addAll(roomInfo.getData()); mCategoryAdapter.setDataEntities(mDataEntities); } }, throwable -> { }); }
Example #12
Source File: VrVideoFragment.java From GoogleVR with Apache License 2.0 | 6 votes |
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { // super.onViewCreated(view, savedInstanceState); recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); RecyclerView.LayoutManager layoutManager = getLayoutManager(); recyclerView.setLayoutManager(layoutManager); OkGo.get(ApiUrls.URL_Query).cacheKey(ApiUrls.URL_Query).cacheMode(CacheMode.DEFAULT).execute(new StringCallback() { @Override public void onSuccess(String s, Call call, Response response) { try { JSONObject obj = new JSONObject(s); String content = obj.getString("content"); List<VideoItem> videoItems = JSON.parseArray(content, VideoItem.class); recyclerView.setAdapter(new VrVideoAdapter(videoItems)); } catch (JSONException e) { e.printStackTrace(); } } }); }
Example #13
Source File: Request.java From okhttp-OkGo with Apache License 2.0 | 6 votes |
public Request(String url) { this.url = url; baseUrl = url; OkGo go = OkGo.getInstance(); //默认添加 Accept-Language String acceptLanguage = HttpHeaders.getAcceptLanguage(); if (!TextUtils.isEmpty(acceptLanguage)) headers(HttpHeaders.HEAD_KEY_ACCEPT_LANGUAGE, acceptLanguage); //默认添加 User-Agent String userAgent = HttpHeaders.getUserAgent(); if (!TextUtils.isEmpty(userAgent)) headers(HttpHeaders.HEAD_KEY_USER_AGENT, userAgent); //添加公共请求参数 if (go.getCommonParams() != null) params(go.getCommonParams()); if (go.getCommonHeaders() != null) headers(go.getCommonHeaders()); //添加缓存模式 retryCount = go.getRetryCount(); cacheMode = go.getCacheMode(); cacheTime = go.getCacheTime(); }
Example #14
Source File: CategoryFragment.java From MyHearts with Apache License 2.0 | 6 votes |
private void refreshData(String url) { OkGo.get(url) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<RoomInfo>() { }.getType(); RoomInfo roomInfo = new Gson().fromJson(s, type); if (roomInfo.getData().size() > 0) { mDataEntities.clear(); mDataEntities.addAll(roomInfo.getData()); mCategoryAdapter.setDataEntities(mDataEntities); } }, throwable -> { }); }
Example #15
Source File: LordFragment.java From MyHearts with Apache License 2.0 | 6 votes |
private void initLordData() { OkGo.post(HttpUrlPaths.LORD_CATEGORY) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<LordBean>() { }.getType(); LordBean bean = new Gson().fromJson(s, type); if (bean.getErrorCode() == 0 && bean.getErrorStr().equals("success") && bean.getResultCount() > 0) { mLordDatas = bean.getResults(); mLordAdapter.setDatas(mLordDatas); mLordAdapter.notifyDataSetChanged(); } }, throwable -> { }); }
Example #16
Source File: LordFragment.java From MyHearts with Apache License 2.0 | 6 votes |
/** * 刷新数据 */ private void refreshData() { OkGo.post(HttpUrlPaths.LORD_CATEGORY) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<LordBean>() { }.getType(); LordBean bean = new Gson().fromJson(s, type); if (bean.getErrorCode() == 0 && bean.getErrorStr().equals("success") && bean.getResultCount() > 0) { mLordDatas.clear(); mLordDatas = bean.getResults(); //mLordAdapter.a(mLordDatas); mLordAdapter.setDatas(mLordDatas); mLordRefresh.setRefreshing(false); } }, throwable -> { }); }
Example #17
Source File: TestActivity.java From okhttp-OkGo with Apache License 2.0 | 6 votes |
@OnClick(R.id.btn2) public void btn2(View view) { new Thread(new Runnable() { @Override public void run() { try { Call<JSONObject> adapt = OkGo.<JSONObject>get(Urls.URL_JSONOBJECT).adapt(); Response<JSONObject> response = adapt.execute(); System.out.println("body " + response.body()); Throwable exception = response.getException(); if (exception != null) exception.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
Example #18
Source File: LordDetailActivity.java From MyHearts with Apache License 2.0 | 6 votes |
private void initRefresh(MaterialRefreshLayout materialRefreshLayout) { page = 1; //重置 在刷新的时候 OkGo.post(HttpUrlPaths.LORD_DETAIL_URL) .params("catgId", catgId) .params("page", page) .params("userid", 0) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<LordDetailBean>() { }.getType(); LordDetailBean bean = new Gson().fromJson(s, type); if (bean.getErrorStr().equals("success") && bean.getErrorCode() == 0 && bean.getResultCount() > 0) { mLordDetailDatas.clear(); detailAdapter.notifyDataSetChanged(); mLordRefresh.finishRefresh(); } }, throwable -> { }); }
Example #19
Source File: LordDetailActivity.java From MyHearts with Apache License 2.0 | 6 votes |
/** * 请求数据 */ private void initLordDetailData() { OkGo.post(HttpUrlPaths.LORD_DETAIL_URL) .params("catgId", catgId) .params("page", page) .params("userid", 0) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<LordDetailBean>() { }.getType(); LordDetailBean bean = new Gson().fromJson(s, type); if (bean.getErrorStr().equals("success") && bean.getErrorCode() == 0 && bean.getResultCount() > 0) { mLordDetailDatas = bean.getResults(); detailAdapter.addData(mLordDetailDatas); } }, throwable -> { }); }
Example #20
Source File: CircleFriendsActivity.java From MyHearts with Apache License 2.0 | 5 votes |
/** * 加载更多数据 */ private void getMoreData() { page++; if (page >= totalPage + 1) { Toast.makeText(this, getResources().getString(R.string.loading_finish), Toast.LENGTH_SHORT).show(); CustomPrograss.disMiss(); return; } OkGo.get(HttpUrlPaths.THOUGHTS_DETAIL_COMMENT_URL) .params("type", "user_event") .params("eventid", eventid) .params("page", page) .params("userid", 0) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<CircleFriendsCommentBean>() { }.getType(); CircleFriendsCommentBean bean = new Gson().fromJson(s, type); if (bean.getErrorCode() == 0 && bean.getResultCount() > 0 && bean.getErrorStr().equals("success")) { List<CircleFriendsCommentBean.ResultsEntity> results = bean.getResults(); mCircleFriendsComments.addAll(results); //Toast.makeText(this, "mCircleFriendsComments.size():" + mCircleFriendsComments.size(), Toast.LENGTH_SHORT).show(); mCommentAdapter.setDatas(mCircleFriendsComments); CustomPrograss.disMiss(); mScrollView.loadingComponent(); } }, throwable -> { }); }
Example #21
Source File: CategoryFragment.java From MyHearts with Apache License 2.0 | 5 votes |
/** * 获取到更多数据 上拉刷新 */ private void getMoreData() { offset += limit; limit += lordMoreNum; if (mIndex == 0) { url = HttpUrlPaths.getDouyuLiveChannel(limit, offset); } else { url = HttpUrlPaths.getDouyuSubChannelBaseTag(mIndex, limit, offset); } OkGo.get(url) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<RoomInfo>() { }.getType(); RoomInfo roomInfo = new Gson().fromJson(s, type); if (roomInfo.getData().size() > 0) { mDataEntities.addAll(roomInfo.getData()); mCategoryAdapter.setDataEntities(mDataEntities); mCategoryAdapter.notifyItemRemoved(mCategoryAdapter.getItemCount()); } }, throwable -> { }); }
Example #22
Source File: GroupMemberActivity.java From MyHearts with Apache License 2.0 | 5 votes |
/** * 刷新数据 */ private void refreshData() { page = 1; if (!TextUtils.isEmpty(groupid)) { OkGo.get(HttpUrlPaths.LORD_MEMBER_DETAILE_URL) .params("page", page) .params("groupid", groupid) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<GroupMemberBean>() { }.getType(); GroupMemberBean bean = new Gson().fromJson(s, type); if (bean.getErrorCode() == 0 && bean.getErrorStr().equals("success") && bean.getResultCount() > 0) { mMemberDatas.clear(); mMemberDatas = bean.getResults(); mMemberAdapter.addData(mMemberDatas); mRefreshLayout.setRefreshing(false); } }, throwable -> { }); } }
Example #23
Source File: BaseOkgoApi.java From BaseProject with Apache License 2.0 | 5 votes |
protected static Request createRequest(String wholeUrl, byte requestMethod) { switch (requestMethod) { case GET: return OkGo.get(wholeUrl); case POST: return OkGo.post(wholeUrl); case PUT: return OkGo.put(wholeUrl); } return null; }
Example #24
Source File: NewFragment.java From MyHearts with Apache License 2.0 | 5 votes |
/** * 请求数据 */ private void initData() { //labelid=0&type=1&page=1&userid=0 OkGo.post(HttpUrlPaths.THOUGHTS_URL) .params("userid", 54442) .params("labelid", catgId) .params("type", 2) .params("page", 1) .getCall(StringConvert.create(), RxAdapter.<String>create()) .doOnSubscribe(() -> { }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Type type = new TypeToken<ThoughtsBean>() { }.getType(); ThoughtsBean bean = new Gson().fromJson(s, type); if (bean.getErrorCode() == 0 && bean.getResultCount() > 0 && bean.getErrorStr().equals("success")) { mThroughtDatas = bean.getResults(); mThroughtAdapter.setResultsBeen(mThroughtDatas); // mRefreshLayout.setLoadMore(true); // mRefreshLayout.setClickable(true); } }, throwable -> { }); }
Example #25
Source File: CacheActivity.java From okhttp-OkGo with Apache License 2.0 | 5 votes |
@OnClick(R.id.first_cache_then_request) public void first_cache_then_request(View view) { OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)// .tag(this)// .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)// .cacheKey("first_cache_then_request")// .cacheTime(5000) // 单位毫秒.5秒后过期 .headers("header1", "headerValue1")// .params("param1", "paramValue1")// .execute(new CacheCallBack(this)); }
Example #26
Source File: CacheActivity.java From okhttp-OkGo with Apache License 2.0 | 5 votes |
@OnClick(R.id.if_none_cache_request) public void if_none_cache_request(View view) { OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)// .tag(this)// .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)// .cacheKey("if_none_cache_request")// .cacheTime(5000) // 单位毫秒.5秒后过期 .headers("header1", "headerValue1")// .params("param1", "paramValue1")// .execute(new CacheCallBack(this)); }
Example #27
Source File: CacheActivity.java From okhttp-OkGo with Apache License 2.0 | 5 votes |
@OnClick(R.id.request_failed_read_cache) public void request_failed_read_cache(View view) { OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)// .tag(this)// .cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)// .cacheKey("request_failed_read_cache")// .cacheTime(5000) // 单位毫秒.5秒后过期 .headers("header1", "headerValue1")// .params("param1", "paramValue1")// .execute(new CacheCallBack(this)); }
Example #28
Source File: CacheActivity.java From okhttp-OkGo with Apache License 2.0 | 5 votes |
@OnClick(R.id.cache_default) public void cache_default(View view) { OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)// .tag(this)// .cacheMode(CacheMode.DEFAULT)// .cacheKey("cache_default")// .cacheTime(5000)//对于默认的缓存模式,该时间无效,依靠的是服务端对304缓存的控制 .headers("header1", "headerValue1")// .params("param1", "paramValue1")// .execute(new CacheCallBack(this)); }
Example #29
Source File: CacheActivity.java From okhttp-OkGo with Apache License 2.0 | 5 votes |
@OnClick(R.id.no_cache) public void no_cache(View view) { OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)// .tag(this)// .cacheMode(CacheMode.NO_CACHE)// .cacheKey("no_cache") //对于无缓存模式,该参数无效 .cacheTime(5000) //对于无缓存模式,该时间无效 .headers("header1", "headerValue1")// .params("param1", "paramValue1")// .execute(new CacheCallBack(this)); }
Example #30
Source File: CookieActivity.java From okhttp-OkGo with Apache License 2.0 | 5 votes |
@OnClick(R.id.removeCookie) public void removeCookie(View view) { HttpUrl httpUrl = HttpUrl.parse(Urls.URL_METHOD); CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore(); cookieStore.removeCookie(httpUrl); showToast("详细移除cookie的代码,请看demo的代码"); }