java 调用接口代码实例

package com.zhuoshi.jcbigdata.spark.jinjingzheng;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;

import net.sf.json.JSONObject;

/**
 * @author lx
 * @describe 调用进京证违法借口
 *
 */
public class illegalInterfaceService {
    public void illegalInfo(String rowkey) throws Exception{
    	
	String url="http://xxxxxxxxx/taoPaiChe/taoPaiCheFenxiPage?rowkey="+rowkey;
	 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.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();
     //解析嵌套JSON
     JSONObject jsonobject = JSONObject.fromObject(new String(resultStr.toString()));
     jsonobject.get("illegalinfo");
   } 
}


参考:[https://www.cnblogs.com/angusbao/p/7677621.html](https://www.cnblogs.com/angusbao/p/7677621.html)

你可能感兴趣的:(java,接口调用,java接口调用)