Home > Java > JavaBase > body text

How to call the java interface (with code)

Release: 2019-11-26 09:28:21
Original
3354 people have browsed it

How to call the java interface (with code)

java calls the interface method:

package com.lwl.api.provider.demo.test;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class RestUtil {
    public String load(String url,String query) throws Exception
    {
        URL restURL = new URL(url);
        /*
         * 此处的urlConnection对象实际上是根据URL的请求协议(此处是http)生成的URLConnection类 的子类HttpURLConnection
         */
        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
        //请求方式
        conn.setRequestMethod("POST");
        //设置是否从httpUrlConnection读入,默认情况下是true; httpUrlConnection.setDoInput(true);
        conn.setDoOutput(true);
        //allowUserInteraction 如果为 true,则在允许用户交互(例如弹出一个验证对话框)的上下文中对此 URL 进行检查。
        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("接口调用数据:"+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) {
        System.out.print(e.getMessage());
    }
    }
}
Copy after login

First URL restURL = new URL(url); the url in this is the target interface address that needs to be adjusted, URL Class is a class under java.net.*, which is not unfamiliar.

setRequestMethod("POST"); The request method has two values ​​to choose from, one is GET and the other is POST. Select the corresponding request method

setDoOutput(true);setDoInput(true );

setDoInput() : // Set whether to output to httpUrlConnection. Because this is a post request, the parameters must be placed in the http body, so it needs to be set to true. The default is false;

setDoOutput(): //Set whether to read from httpUrlConnection, which is true by default;

setAllowUserInteraction();allowUserInteraction If true, in a context that allows user interaction (such as popping up a verification dialog box) Check this URL.

The query in the following code is transmitted in the form of attribute=value. If there are multiple queries, it is transmitted in the form of attribute=value&attribute=value. It is passed to the server and let the server handle it by itself.

How to generate this form? The simplest and fastest way is here: Java splices the Map into "parameter = value & parameter = value"

close(); Create a stream to write or read the return value. Remember to close the stream after creation .

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of How to call the java interface (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template