Example tutorial of calling interface in java
Nov 13, 2019 am 11:50 AM1. First URL restURL = new URL(url)
; The url is the target interface address that needs to be adjusted, and the URL class is java.net.* category under.
2, setRequestMethod("POST")
; The request method has two values ??to choose from, one is GET and the other is POST, select the corresponding request method
3. setDoOutput(true);setDoInput(true)
;
setDoInput(): // 設(shè)置是否向httpUrlConnection輸出,因?yàn)檫@個(gè)是post請(qǐng)求,參數(shù)要放在http正文內(nèi), 因此需要設(shè)為true, 默認(rèn)是false; setDoOutput(): // 設(shè)置是否從httpUrlConnection讀入,默認(rèn)情況下是true;
4. setAllowUserInteraction();allowUserInteraction
If true, user interaction is allowed (such as pop-up This URL is checked within the context of a validation dialog box.
5. The query in the following code is transmitted in the form of "attribute=value". If there are multiple queries, it is passed in the form of "attribute=value&attribute=value". It is passed to the server and let the server handle it by itself.
6, close();
Create a stream to write or read the return value. Remember to close the stream after creation.
Example tutorial:
package com.c; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; public class RestUtil { public String load(String url,String query) throws Exception { URL restURL = new URL(url); /* * 此處的urlConnection對(duì)象實(shí)際上是根據(jù)URL的請(qǐng)求協(xié)議(此處是http)生成的URLConnection類 的子類 HttpURLConnection */ HttpURLConnection conn = (HttpURLConnection) restURL.openConnection(); //請(qǐng)求方式 conn.setRequestMethod("POST"); //設(shè)置是否從httpUrlConnection讀入,默認(rèn)情況下是true; httpUrlConnection.setDoInput(true); conn.setDoOutput(true); //allowUserInteraction 如果為 true,則在允許用戶交互(例如彈出一個(gè)驗(yàn)證對(duì)話框)的上下文中對(duì)此 URL 進(jìn)行檢查。 conn.setAllowUserInteraction(false); PrintStream ps = new PrintStream(conn.getOutputStream()); ps.print(query); ps.close(); BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line,resultStr=""; while(null != (line=bReader.readLine())) { resultStr +=line; } System.out.println("3412412---"+resultStr); bReader.close(); return resultStr; } public static void main(String []args) {try { RestUtil restUtil = new RestUtil(); String resultString = restUtil.load( "http://192.168.10.89:8080/eoffice-restful/resources/sys/oaholiday", "floor=first&year=2017&month=9&isLeader=N"); } catch (Exception e) { // TODO: handle exception System.out.print(e.getMessage()); } } }
Recommended tutorial:Java tutorial
The above is the detailed content of Example tutorial of calling interface in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

itertools.combinations is used to generate all non-repetitive combinations (order irrelevant) that selects a specified number of elements from the iterable object. Its usage includes: 1. Select 2 element combinations from the list, such as ('A','B'), ('A','C'), etc., to avoid repeated order; 2. Take 3 character combinations of strings, such as "abc" and "abd", which are suitable for subsequence generation; 3. Find the combinations where the sum of two numbers is equal to the target value, such as 1 5=6, simplify the double loop logic; the difference between combinations and arrangement lies in whether the order is important, combinations regard AB and BA as the same, while permutations are regarded as different;

java.lang.OutOfMemoryError: Javaheapspace indicates insufficient heap memory, and needs to check the processing of large objects, memory leaks and heap settings, and locate and optimize the code through the heap dump analysis tool; 2. Metaspace errors are common in dynamic class generation or hot deployment due to excessive class metadata, and MaxMetaspaceSize should be restricted and class loading should be optimized; 3. Unabletocreatenewnativethread due to exhausting system thread resources, it is necessary to check the number of threads, use thread pools, and adjust the stack size; 4. GCoverheadlimitexceeded means that GC is frequent but has less recycling, and GC logs should be analyzed and optimized.

fixture is a function used to provide preset environment or data for tests. 1. Use the @pytest.fixture decorator to define fixture; 2. Inject fixture in parameter form in the test function; 3. Execute setup before yield, and then teardown; 4. Control scope through scope parameters, such as function, module, etc.; 5. Place the shared fixture in conftest.py to achieve cross-file sharing, thereby improving the maintainability and reusability of tests.

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear
