java 的调用接口

public void farm(){
  try {
  //调用方法
    farmPort(FARM_PORT_URL,
            "loginName="+1+
                    "&companyIphone="+2+
                    "&companyName="+3+
                    "&idCard="+4+
                    "&companyPassword="+5);
  } catch (IOException e) {
   log.error("农机接口创建用户异常",e.getStackTrace());
   e.printStackTrace();
    throw new GlobalException(new CodeMsg(500, "用户创建出现异常请稍后重试"));
  }
}

//接口操作
public void farmPort(String url,String query) throws IOException {
    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;
    }
    bReader.close();
}

你可能感兴趣的:(java)