In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is OkHttpClientUtil? in view of this question, this article introduces in detail the corresponding analysis and answers, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
OkHttp utility class
/ * * OkHttpClient tool * * @ author yuhao.wang3 * / public abstract class OkHttpClientUtil {private static final Logger logger = LoggerFactory.getLogger (OkHttpClientUtil.class) Private static OkHttpClient okHttpClient = new OkHttpClient.Builder () .connectTimeout (10, TimeUnit.SECONDS) .writeTimeout (10, TimeUnit.SECONDS) .readTimeout (20, TimeUnit.SECONDS) .sslSocketFactory (SSLSocketClient.getSSLSocketFactory (), SSLSocketClient.getTrustManager ()) .hostnameVerifier (SSLSocketClient.getHostnameVerifier ()) .build () / * initiate post request for application/json * * @ param url address * @ param param Parameter * @ param interfaceName API name * @ return * @ throws Exception * / public static T postApplicationJson (String url, Object param, String interfaceName, Class clazz) {/ / generate requestBody RequestBody requestBody = FormBody.create (MediaType.parse ("application/json) Charset=utf-8 "), JSON.toJSONString (param)); return post (url, interfaceName, requestBody, param, null, clazz) } / * initiate post request for application/json * * @ param url address * @ param param Parameter * @ param interfaceName Interface name * @ return * @ throws Exception * / public static T postApplicationJson (String url, Object param, Map header, String interfaceName Class clazz) {/ / generate requestBody RequestBody requestBody = FormBody.create (MediaType.parse ("application/json) Charset=utf-8 "), JSON.toJSONString (param)); return post (url, interfaceName, requestBody, param, header, clazz) } / * initiate post request for x-www-form-urlencoded * * @ param url address * @ param param Parameter * @ param interfaceName Interface name * @ return * @ throws Exception * / public static T postApplicationXWwwFormUrlencoded (String url, Object param, String interfaceName, Class clazz) {Map paramMap = JSON.parseObject (JSON.toJSONString (param)) New TypeReference () {}) / generate requestBody StringBuilder content = new StringBuilder; for (Map.Entry entry: paramMap.entrySet ()) {content.append (entry.getKey ()) .append ("=") .append (entry.getValue ()) .append ("&");} if (content.length () > 0) {content.deleteCharAt (content.length ()-1) } RequestBody requestBody = FormBody.create (MediaType.parse ("application/x-www-form-urlencoded"), content.toString (); return post (url, interfaceName, requestBody, param, null, clazz) } / * initiate a post request URL * @ param interfaceName interface name * @ param requestBody request body * @ param param parameter * / public static T post (String url, String interfaceName, RequestBody requestBody, Object param, Map headers) for sending the request without any signature * * @ param url Class clazz) {Request.Builder builder = new Request.Builder () / / url .url (url) .post (requestBody) of the request If (MapUtils.isNotEmpty (headers)) {for (String key: headers.keySet ()) {builder.addHeader (key, headers.get (key));}} Request request = builder.build (); Response response = null; String result = "; String errorMsg =" Try {/ / create / Call response = okHttpClient.newCall (request). Execute (); if (! response.isSuccessful ()) {logger.error ("access external system exception {}: {}", url, response.toString ()); errorMsg = String.format ("access external system exception:% s", response.toString ()) Throw new RemoteAccessException (errorMsg);} result = response.body (). String ();} catch (RemoteAccessException e) {logger.warn (e.getMessage (), e); result = e.getMessage (); throw e;} catch (Exception e) {logger.warn (e.getMessage (), e) If (Objects.isNull (response)) {errorMsg = String.format ("access external system exception::% s", e.getMessage ()); throw new RemoteAccessException (errorMsg, e);} errorMsg = String.format ("access external system exception:% s", response.toString ()); throw new RemoteAccessException (errorMsg, e) } finally {logger.info ("request {} {}, request parameters: {}, header: {}, return parameters: {}", interfaceName, url, JSON.toJSONString (param), JSON.toJSONString (headers), StringUtils.isEmpty (result)? ErrorMsg: result);} return JSON.parseObject (result, clazz);}} this is the answer to the question about what OkHttpClientUtil is. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.