com.zhy.http.okhttp.OkHttpUtils Java Examples
The following examples show how to use
com.zhy.http.okhttp.OkHttpUtils.
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: NewsDetailActivity.java From fitness_Android with Apache License 2.0 | 6 votes |
private void addNewComment() { String commentText = addCommentET.getText().toString().trim(); if (TextUtils.isEmpty(commentText)) { DisplayToast("请先输入内容"); return; } String url = Constants.BASE_URL + "Comment?method=addNewComment"; OkHttpUtils .post() .url(url) .id(3) .addParams("newsId", newsId + "") .addParams("userId", Constants.USER.getUserId() + "") .addParams("comment", commentText) .addParams("replyUser", replyUsername) .build() .execute(new MyStringCallback()); }
Example #2
Source File: App.java From Socket.io-FLSocketIM-Android with MIT License | 6 votes |
private void initOkHttpUtil () { CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext())); OkHttpClient client = new OkHttpClient.Builder() .connectionPool(new ConnectionPool(5, 5, TimeUnit.MINUTES)) .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); request = request.newBuilder().addHeader("Connection", "keep-alive").build(); return chain.proceed(request); } }) .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .cookieJar(cookieJar) .build(); OkHttpUtils.initClient(client); }
Example #3
Source File: OklaClient.java From iMoney with Apache License 2.0 | 6 votes |
/** * initialize okhttp client config * * @param context */ private void initOkhttpClient(Context context) { if (context == null) return; OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) // .dns(OkHttpDns.getInstance(context)) // .hostnameVerifier(new HostnameVerifier() { // @Override // public boolean verify(String hostname, SSLSession session) { // return hostname.contains("domain.com");// 替换为自己api的域名 // } // }) // .sslSocketFactory(getSSLSocketFactory()) .build(); OkHttpUtils.initClient(okHttpClient); }
Example #4
Source File: MyApplication.java From OpenEyes with Apache License 2.0 | 6 votes |
@Override public void onCreate() { super.onCreate(); Fresco.initialize(this); OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectionSpecs(Arrays.asList( ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT)) .addInterceptor(new LoggerInterceptor("==http")) .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) .build(); OkHttpUtils.initClient(okHttpClient); }
Example #5
Source File: PostFormRequest.java From okhttputils with Apache License 2.0 | 6 votes |
@Override protected RequestBody wrapRequestBody(RequestBody requestBody, final Callback callback) { if (callback == null) return requestBody; CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.Listener() { @Override public void onRequestProgress(final long bytesWritten, final long contentLength) { OkHttpUtils.getInstance().getDelivery().execute(new Runnable() { @Override public void run() { callback.inProgress(bytesWritten * 1.0f / contentLength,contentLength,id); } }); } }); return countingRequestBody; }
Example #6
Source File: PostFileRequest.java From okhttputils with Apache License 2.0 | 6 votes |
@Override protected RequestBody wrapRequestBody(RequestBody requestBody, final Callback callback) { if (callback == null) return requestBody; CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.Listener() { @Override public void onRequestProgress(final long bytesWritten, final long contentLength) { OkHttpUtils.getInstance().getDelivery().execute(new Runnable() { @Override public void run() { callback.inProgress(bytesWritten * 1.0f / contentLength,contentLength,id); } }); } }); return countingRequestBody; }
Example #7
Source File: OtherRequest.java From okhttputils with Apache License 2.0 | 6 votes |
@Override protected Request buildRequest(RequestBody requestBody) { if (method.equals(OkHttpUtils.METHOD.PUT)) { builder.put(requestBody); } else if (method.equals(OkHttpUtils.METHOD.DELETE)) { if (requestBody == null) builder.delete(); else builder.delete(requestBody); } else if (method.equals(OkHttpUtils.METHOD.HEAD)) { builder.head(); } else if (method.equals(OkHttpUtils.METHOD.PATCH)) { builder.patch(requestBody); } return builder.build(); }
Example #8
Source File: RequestCall.java From okhttputils with Apache License 2.0 | 6 votes |
public Call buildCall(Callback callback) { request = generateRequest(callback); if (readTimeOut > 0 || writeTimeOut > 0 || connTimeOut > 0) { readTimeOut = readTimeOut > 0 ? readTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; writeTimeOut = writeTimeOut > 0 ? writeTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; connTimeOut = connTimeOut > 0 ? connTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; clone = OkHttpUtils.getInstance().getOkHttpClient().newBuilder() .readTimeout(readTimeOut, TimeUnit.MILLISECONDS) .writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS) .connectTimeout(connTimeOut, TimeUnit.MILLISECONDS) .build(); call = clone.newCall(request); } else { call = OkHttpUtils.getInstance().getOkHttpClient().newCall(request); } return call; }
Example #9
Source File: RequestCall.java From enjoyshop with Apache License 2.0 | 6 votes |
public Call buildCall(Callback callback) { request = generateRequest(callback); if (readTimeOut > 0 || writeTimeOut > 0 || connTimeOut > 0) { readTimeOut = readTimeOut > 0 ? readTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; writeTimeOut = writeTimeOut > 0 ? writeTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; connTimeOut = connTimeOut > 0 ? connTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; clone = OkHttpUtils.getInstance().getOkHttpClient().newBuilder() .readTimeout(readTimeOut, TimeUnit.MILLISECONDS) .writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS) .connectTimeout(connTimeOut, TimeUnit.MILLISECONDS) .build(); call = clone.newCall(request); } else { call = OkHttpUtils.getInstance().getOkHttpClient().newCall(request); } return call; }
Example #10
Source File: OtherRequest.java From enjoyshop with Apache License 2.0 | 6 votes |
@Override protected Request buildRequest(RequestBody requestBody) { if (method.equals(OkHttpUtils.METHOD.PUT)) { builder.put(requestBody); } else if (method.equals(OkHttpUtils.METHOD.DELETE)) { if (requestBody == null) builder.delete(); else builder.delete(requestBody); } else if (method.equals(OkHttpUtils.METHOD.HEAD)) { builder.head(); } else if (method.equals(OkHttpUtils.METHOD.PATCH)) { builder.patch(requestBody); } return builder.build(); }
Example #11
Source File: PostFileRequest.java From enjoyshop with Apache License 2.0 | 6 votes |
@Override protected RequestBody wrapRequestBody(RequestBody requestBody, final Callback callback) { if (callback == null) return requestBody; CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.Listener() { @Override public void onRequestProgress(final long bytesWritten, final long contentLength) { OkHttpUtils.getInstance().getDelivery().execute(new Runnable() { @Override public void run() { callback.inProgress(bytesWritten * 1.0f / contentLength,contentLength,id); } }); } }); return countingRequestBody; }
Example #12
Source File: MainActivity.java From okhttputils with Apache License 2.0 | 6 votes |
public void postFile(View view) { File file = new File(Environment.getExternalStorageDirectory(), "messenger_01.png"); if (!file.exists()) { Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show(); return; } String url = mBaseUrl + "user!postFile"; OkHttpUtils .postFile() .url(url) .file(file) .build() .execute(new MyStringCallback()); }
Example #13
Source File: PostFormRequest.java From enjoyshop with Apache License 2.0 | 6 votes |
@Override protected RequestBody wrapRequestBody(RequestBody requestBody, final Callback callback) { if (callback == null) return requestBody; CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.Listener() { @Override public void onRequestProgress(final long bytesWritten, final long contentLength) { OkHttpUtils.getInstance().getDelivery().execute(new Runnable() { @Override public void run() { callback.inProgress(bytesWritten * 1.0f / contentLength,contentLength,id); } }); } }); return countingRequestBody; }
Example #14
Source File: app.java From ClassSchedule with Apache License 2.0 | 6 votes |
private void initOkHttp() { ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext())); Cache.instance().setCookieJar(cookieJar); OkHttpClient okHttpClient = new OkHttpClient.Builder() //.followRedirects(false) //禁制OkHttp的重定向操作,我们自己处理重定向 .followSslRedirects(false) //.cookieJar(new LocalCookieJar()) //为OkHttp设置自动携带Cookie的功能 .addInterceptor(new LoggerInterceptor("TAG")) //.cookieJar(new CookieJarImpl(new PersistentCookieStore(getBaseContext()))) //要在内存Cookie前 //.cookieJar(new CookieJarImpl(new MemoryCookieStore()))//内存Cookie .cookieJar(cookieJar) .cache(null) .build(); OkHttpUtils.initClient(okHttpClient); }
Example #15
Source File: MainActivity.java From okhttputils with Apache License 2.0 | 6 votes |
public void multiFileUpload(View view) { File file = new File(Environment.getExternalStorageDirectory(), "messenger_01.png"); File file2 = new File(Environment.getExternalStorageDirectory(), "test1#.txt"); if (!file.exists()) { Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show(); return; } Map<String, String> params = new HashMap<>(); params.put("username", "张鸿洋"); params.put("password", "123"); String url = mBaseUrl + "user!uploadFile"; OkHttpUtils.post()// .addFile("mFile", "messenger_01.png", file)// .addFile("mFile", "test1.txt", file2)// .url(url) .params(params)// .build()// .execute(new MyStringCallback()); }
Example #16
Source File: MemoryCallBack.java From GSYVideoPlayer with Apache License 2.0 | 5 votes |
public boolean saveFile(Response response, final int id) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len = 0; try { is = response.body().byteStream(); final long total = response.body().contentLength(); long sum = 0; while ((len = is.read(buf)) != -1) { sum += len; final long finalSum = sum; OkHttpUtils.getInstance().getDelivery().execute(new Runnable() { @Override public void run() { Debuger.printfLog("######### inProgress" + finalSum * 1.0f / total); inProgress(finalSum * 1.0f / total, total, id); } }); } return true; } finally { try { response.body().close(); if (is != null) is.close(); } catch (IOException e) { } } }
Example #17
Source File: OkhttpFactory.java From iMoney with Apache License 2.0 | 5 votes |
/** * okhttp post request (overload method). * * @param url * @param json * @param listener */ public static void post(String url, String json, ApiRequestListener listener) { OkHttpUtils .postString() .url(url) .content(json) .mediaType(MediaType.parse("application/json; charset=utf-8")) .build() .execute(new RequestStringCallback(listener)); }
Example #18
Source File: MyApplication.java From XBanner with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); ViewTarget.setTagId(R.id.glide_tag); //Fresco初始化 Fresco.initialize(this); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(new LoggerInterceptor("Xbanner")) .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) .build(); OkHttpUtils.initClient(okHttpClient); Utils.init(this); }
Example #19
Source File: OkhttpFactory.java From iMoney with Apache License 2.0 | 5 votes |
/** * okhttp post request. * * @param url * @param map * @param listener */ public static void post(String url, HashMap<String, Object> map, ApiRequestListener listener) { OkHttpUtils .postString() .url(url) .content(JsonUtil.toJson(map)) .mediaType(MediaType.parse("application/json; charset=utf-8")) .build() .execute(new RequestStringCallback(listener)); }
Example #20
Source File: AppNetConfig.java From OpenWeatherPlus-Android with Apache License 2.0 | 5 votes |
/** * post请求 * * @param url * @param params * @param headers 请求头是非必传字段,如果没有,设置null即可 * @param callback */ public static final void RequestPost(final String url, final int writeTimeOut, final HashMap<String, String> params, final HashMap<String, String> headers, final StringCallback callback) { if ( headers == null ) { OkHttpUtils.post().url(url).params(params).build().readTimeOut(writeTimeOut).execute(callback); } else { OkHttpUtils.post().url(url).headers(headers).params(params).build().readTimeOut(writeTimeOut).execute(callback); } }
Example #21
Source File: MainActivity.java From okhttputils with Apache License 2.0 | 5 votes |
public void postString(View view) { String url = mBaseUrl + "user!postString"; OkHttpUtils .postString() .url(url) .mediaType(MediaType.parse("application/json; charset=utf-8")) .content(new Gson().toJson(new User("zhy", "123"))) .build() .execute(new MyStringCallback()); }
Example #22
Source File: MainActivity.java From okhttputils with Apache License 2.0 | 5 votes |
public void getHttpsHtml(View view) { String url = "https://kyfw.12306.cn/otn/"; // "https://12 // url =3.125.219.144:8443/mobileConnect/MobileConnect/authLogin.action?systemid=100009&mobile=13260284063&pipe=2&reqtime=1422986580048&ispin=2"; OkHttpUtils .get()// .url(url)// .id(101) .build()// .execute(new MyStringCallback()); }
Example #23
Source File: MainActivity.java From okhttputils with Apache License 2.0 | 5 votes |
public void uploadFile(View view) { File file = new File(Environment.getExternalStorageDirectory(), "messenger_01.png"); if (!file.exists()) { Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show(); return; } Map<String, String> params = new HashMap<>(); params.put("username", "张鸿洋"); params.put("password", "123"); Map<String, String> headers = new HashMap<>(); headers.put("APP-Key", "APP-Secret222"); headers.put("APP-Secret", "APP-Secret111"); String url = mBaseUrl + "user!uploadFile"; OkHttpUtils.post()// .addFile("mFile", "messenger_01.png", file)// .url(url)// .params(params)// .headers(headers)// .build()// .execute(new MyStringCallback()); }
Example #24
Source File: MainActivity.java From okhttputils with Apache License 2.0 | 5 votes |
public void clearSession(View view) { CookieJar cookieJar = OkHttpUtils.getInstance().getOkHttpClient().cookieJar(); if (cookieJar instanceof CookieJarImpl) { ((CookieJarImpl) cookieJar).getCookieStore().removeAll(); } }
Example #25
Source File: MyApplication.java From okhttputils with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); ClearableCookieJar cookieJar1 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext())); HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null); // CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore()); OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) .addInterceptor(new LoggerInterceptor("TAG")) .cookieJar(cookieJar1) .hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }) .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager) .build(); OkHttpUtils.initClient(okHttpClient); }
Example #26
Source File: MainActivity.java From okhttputils with Apache License 2.0 | 5 votes |
public void getHtml(View view) { String url = "http://www.zhiyun-tech.com/App/Rider-M/changelog-zh.txt"; url="http://www.391k.com/api/xapi.ashx/info.json?key=bd_hyrzjjfb4modhj&size=10&page=1"; OkHttpUtils .get() .url(url) .id(100) .build() .execute(new MyStringCallback()); }
Example #27
Source File: OkhttpFactory.java From iMoney with Apache License 2.0 | 5 votes |
/** * okhttp get request. * * @param url * @param listener */ public static void get(String url, ApiRequestListener listener) { OkHttpUtils .get() .url(url) .build() .execute(new RequestStringCallback(listener)); }
Example #28
Source File: App.java From LiuAGeAndroid with MIT License | 5 votes |
@Override public void onCreate() { super.onCreate(); app = this; // 存放所有activity的集合 mActivityList = new ArrayList<>(); // 初始化app异常处理器 - 打包的时候开启 // CrashHandler handler = CrashHandler.getInstance(); // handler.init(getApplicationContext()); // 初始化OkHttpUtils OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) //其他配置 .build(); OkHttpUtils.initClient(okHttpClient); // 初始化Fresco ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this) .setProgressiveJpegConfig(new SimpleProgressiveJpegConfig()) .build(); Fresco.initialize(this, config); // 初始化ShareSDK ShareSDK.initSDK(this); // 初始化JPush JPushInterface.setDebugMode(true); JPushInterface.init(this); // 更新用户登录状态 UserBean.updateUserInfoFromNetwork(new UserBean.OnUpdatedUserInfoListener()); }
Example #29
Source File: RequestCall.java From enjoyshop with Apache License 2.0 | 5 votes |
public void execute(Callback callback) { buildCall(callback); if (callback != null) { callback.onBefore(request, getOkHttpRequest().getId()); } OkHttpUtils.getInstance().execute(this, callback); }
Example #30
Source File: NewsDetailActivity.java From fitness_Android with Apache License 2.0 | 5 votes |
private void addNewFavor() { String url = Constants.BASE_URL + "Favor?method=addNewFavor"; OkHttpUtils .post() .url(url) .id(2) .addParams("newsId", newsId + "") .addParams("userId", Constants.USER.getUserId() + "") .build() .execute(new MyStringCallback()); }