怎么根据企业信用代码获取省市区代码

怎么根据批量使用企业信用代码获取省市区,帮忙解决一下Java语言和Scala语言都行

以下是一个Java的示例代码,使用了阿里云的企业信息查询API,根据企业信用代码获取对应的省市区信息:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class CompanyInfo {
    public static void main(String[] args) {
        String url = "https://ali-companyinfo.showapi.com/cominfo";
        String appcode = "your_app_code";
        String creditCode = "企业信用代码";

        try {
            creditCode = URLEncoder.encode(creditCode, "utf-8");
            String apiUrl = url + "?code=" + creditCode;
            URL obj = new URL(apiUrl);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Authorization", "APPCODE " + appcode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            Map<String, String> resultMap = new HashMap<>();
            String result = response.toString();
            result = result.substring(1, result.length() - 1);
            String[] results = result.split(",");
            for (String res : results) {
                String[] temp = res.split(":");
                resultMap.put(temp[0], temp[1]);
            }

            String province = resultMap.get("province");
            String city = resultMap.get("city");
            String area = resultMap.get("district");
            System.out.println(province + "," + city + "," + area);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

其中需要替换的内容为:

  • your_app_code:替换成你在阿里云上申请的企业信息查询API的AppCode。
  • creditCode:替换成你需要查询的企业信用代码。

注意:使用该API需要先在阿里云上申请企业信息查询API服务,并获取相应的AppCode。

要根据批量使用企业信用代码获取省市区信息,您可以使用第三方的接口或者API

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class CompanyInfo {
    private static String URL_GET_COMPANY_INFO = "http://api.tianyancha.com/services/v3/open/searchV2";

    public static void main(String[] args) {
        String[] codes = {"91310115MA1H9W9M9H", "91350100MA2RNNMM1F", "913101163409295693"};
        for (String code : codes) {
            Company company = getCompanyInfo(code);
            System.out.println(company.toString());
        }
    }

    public static Company getCompanyInfo(String code) {
        Company company = new Company();
        try {
            String param = "word=" + URLEncoder.encode(code, "UTF-8") + "&pageSize=1&pageNum=1";
            String urlNameString = URL_GET_COMPANY_INFO + "?" + param;
            URL url = new URL(urlNameString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();
            connection.disconnect();
            // 解析JSON数据,获取省市区信息
            JSONObject json = new JSONObject(result.toString());
            JSONArray array = json.getJSONArray("data");
            if (array != null && array.length() > 0) {
                JSONObject data = array.getJSONObject(0);
                if (data != null) {
                    company.setName(data.optString("name", ""));
                    company.setCreditCode(data.optString("creditCode", ""));
                    JSONObject regLocation = data.optJSONObject("regLocation");
                    if (regLocation != null) {
                        company.setProvince(regLocation.optString("province", ""));
                        company.setCity(regLocation.optString("city", ""));
                        company.setDistrict(regLocation.optString("district", ""));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return company;
    }
}

class Company {
    private String name;
    private String creditCode;
    private String province;
    private String city;
    private String district;

    // getter and setter methods

    @Override
    public String toString() {
        return "Company{" +
                "name='" + name + '\'' +
                ", creditCode='" + creditCode + '\'' +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", district='" + district + '\'' +
                '}';
    }
}