Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the guidelines for using mica-http

Shulou Source: shulou.com Published: 2022-06-02 02:05:51 09月30日 Update

This article shows you what the guidelines for the use of mica-http are, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can gain something through the detailed introduction of this article.

Mica-http

Mica-http is the encapsulation of okhttp, http toolkit for Fluent syntax, and syntax reference HttpClient Fluent API.

Use maven net.dreamlu mica-http ${version} gradlecompile ("net.dreamlu:mica-http:$ {version}") to use documents

Set the global log level NONE,BASIC,HEADERS,BODY. Default: NONE

HttpRequest.setGlobalLog (LogLevel.BODY)

Synchronous request url. Methods support get, post, patch, put, and delete.

HttpRequest.get ("https://www.baidu.com") .log (LogLevel.BASIC) / / set the log level this time Takes precedence over the global .addHeader ("x-account-id", "mica001") / / adds header .addCookie (new Cookie.Builder () / / adds cookie .name ("sid") .value ("mica_user_001") .build () .query ("Q", "mica") / / sets the url parameter Url encode .queryEncoding ("name", "encodedValue") .formBuilder () / form constructor is performed by default, and the same multipartFormBuilder file upload form .add ("id", 123123) / / form parameter .execute () / initiates the request .asJsonNode () / / result set conversion. Note: if there is a network exception, an exception will be thrown directly. / / similar methods include asString, asBytes, asStream// json response: asJsonNode, asValue, asList, asMap, jackson processing / / xml, html response: asDocument,asDomValue, asDomList jsoup processing / / file file: toFile

Synchronization

String html = HttpRequest.post ("https://www.baidu.com") .execute () .onFailed ((request, e)-> consumption handling of abnormal cases such as {/ / network, but without e.printStackTrace ();}) .onResponse (ResponseSpec::asString); / / handle responses, return null directly with network exceptions, etc.

Synchronization

String text = HttpRequest.patch ("https://www.baidu.com"). Execute () .onsuccess (ResponseSpec::asString); / / onSuccess http code in [200.300) handles the response and returns null directly if there is a network exception.

Asynchronous request

HttpRequest.delete ("https://www.baidu.com") .async () / / enable async. OnFailed ((request, e)-> handling e.printStackTrace () in case of {/ / exception;}) .onResponse (responseSpec-> {/ / consumption response, note: the response stream can only read int httpCode = responseSpec.code () once) }) .onSuccessful (responseSpec-> {/ / consumption response successful http code in [200.300) / Note: the response result stream can only be read once, JsonNode jsonNode = responseSpec.asJsonNode ();}) .execute (); / / Asynchronous last launch request sample code 1Accord / sets the global log level HttpRequest.setGlobalLog (LogLevel.BODY) / directly use the jackson json path syntax private String getUserEmail (String accessToken) {return HttpRequest.get ("https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))") .addHeader (" Host "," api.linkedin.com ") .addHeader (" Connection "," Keep-Alive ") .addHeader (" Authorization ") "Bearer" + accessToken) .execute () .asJsonNode () .at ("/ elements/0/handle~0/emailAddress") .asText () } / / Asynchronous public static void test () {HttpRequest.post ("https://www.baidu.com/do-stuff") .log (LogLevel.BASIC) / / log level .formBuilder () / / form constructor .add (" a ") "b") .async () / / use async .onSuccessful (System.out::println) / / function when async succeeds. OnFailed ((request, e)-> {/ / Asynchronous failure Can not have e.printStackTrace () }) .execute ();} sample code 2HttpRequest.setGlobalLog (LogLevel.BODY) / / synchronization Return nullString html = HttpRequest.get ("www.baidu.com") .connectTimeout (Duration.ofSeconds (1000)) .query ("test", "a") .Query ("name", "three") .Query ("x", 1) .query ("abd", Base64Util.encode ("123 percent percent")) .queryEncoding ("abc", Base64Util.encode ("123 requests percent")) .execute () .onFailed (request)) E)-> {e.printStackTrace () ) .onsuccess (ResponseSpec::asString); System.out.println (html); / / synchronous call, which returns Optional, and returns Optional.empty () Optional opt = HttpRequest.post in case of exception (URI.create ("https://www.baidu.com")) .bodyString (" Important stuff ") .formBuilder () .add (" a "," b ") .execute () .onSuccessOpt (ResponseSpec::asString)) / / synchronization, consumption (processing) responseHttpRequest.post upon success ("https://www.baidu.com/some-form") .addHeader (" X-Custom-header "," stuff ") .execute () .onSuccessful (responseSpec-> {String text = responseSpec.asString (); System.out.println (text);}) / / synchronous, throw HttpRequest.get directly on exception ("https://www.baidu.com/some-form") .execute () .asString (); / / async, asynchronously execute the result, and print stack HttpRequest.get on failure (" https://www.baidu.com/some-form") .async () .onSuccessful (System.out::println) .onFailed ((request, e)-> {e.printStackTrace ()) ") }) .execute (); what are the guidelines for the use of mica-http? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Synchronization processing log level network form consumption success global result syntax usage guide guide code content parameter class skill file method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Microsoft Apple Redmi