通过传递字符串作为Java的输入,使用本地库将Golang的地图返回Java

时间:2019-03-19 11:07:42

标签: java go java-native-interface

我正在尝试从Java程序调用Golang函数。 Reference正在使用。这样就可以很好地工作。

有了这个,我试图将地图从golang返回到java。由于Java中的返回类型为对象/映射,因此无效。实际上,例外情况(可能是编译时)是:

Exception in thread "main" java.lang.IllegalArgumentException: Unsupported return type interface java.util.Map in function GetGeoLoc
        at com.sun.jna.Function.invoke(Function.java:490)
        at com.sun.jna.Function.invoke(Function.java:360)
        at com.sun.jna.Library$Handler.invoke(Library.java:244)
        at com.sun.proxy.$Proxy0.GetGeoLoc(Unknown Source)
        at com.java.readers.GetGeoLoc2Tests.getGeoLoc2(GetGeoLoc2Tests.java:39)
        at com.java.readers.GetGeoLoc2Tests.main(GetGeoLoc2Tests.java:52)

有人帮助我解决问题。我相信我在这里错过了一些东西。

这是代码

Java:

public class GetGeoLoc2Tests {

    static GoCaller GO_CQM_TRANSFORMER;

    public interface GoCaller extends Library {
        public Map<String, String> GetGeoLoc(GoString.ByValue ip);
        public int Add(int i);
        public String SString(GoString.ByValue s);

        public class GoString extends Structure {
            public static class ByValue extends GoString implements Structure.ByValue {
            }

            public String ip;
            public long n;

            protected List getFieldOrder() {
                return Arrays.asList(new String[] { "ip", "n" });
            }
        }
    }

    public static void getGeoLoc2(String ip) {

        if (ip == null) {
            return;
        }

        GoCaller.GoString.ByValue str = new GoCaller.GoString.ByValue();
        str.ip = ip;
        str.n = str.ip.length();

        System.out.println("JAVA----->" + ip);
        Map<String, String> map = GO_CQM_TRANSFORMER.GetGeoLoc(str);
        System.out.println(map);
    }

    public static void main(String[] args) {
        if(args.length == 0) {
            System.out.println("Expecting an input<IpAddress>");
            System.exit(0);
        }

        String pwd = System.getProperty("user.home");
        String lib = pwd + "/go/src/ip2loc/iploc.so";
        GO_CQM_TRANSFORMER = (GoCaller) Native.loadLibrary(lib, GoCaller.class);
//      System.out.println(GO_CQM_TRANSFORMER.Add(10));
        getGeoLoc2(args[0]);
    }

Golang:

package main

import "C"

import (
        "fmt"
        "github.com/ip2location/ip2location-go"
)

func main(){}

//export GetGeoLoc
func GetGeoLoc(ip string) string {
        ip2location.Open("/data/md0/ip2loc/ip6/IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN")
        //ip1 := "8.8.8.8"
        fmt.Println("input:" , ip)
        results := ip2location.Get_all(ip)

        m := make(map[string]string)
        m["country_short"] = results.Country_short
        m["country_long"] = results.Country_long
        m["region"] = results.Region
        m["city"] = results.City
        m["isp"] = results.Isp
        m["latitude"] = fmt.Sprintf("%f", results.Latitude)
        m["longitude"] = fmt.Sprintf("%f", results.Longitude)
        m["domain"] = results.Domain
        m["zipcode"] = results.Zipcode
        m["timezone"] = results.Timezone
        m["netspeed"] = results.Netspeed
        m["iddcode"] = results.Iddcode
        m["areacode"] = results.Areacode
        m["weatherstationcode"] = results.Weatherstationcode
        m["weatherstationaname"] = results.Weatherstationname
        m["mcc"] = results.Mcc
        m["mnc"] = results.Mnc
        m["mobilebrand"] = results.Mobilebrand
        m["elevation"] = fmt.Sprintf("%f", results.Elevation)
        m["usagetype"] = results.Usagetype

        fmt.Println("map:", m)
        ip2location.Close()
        return m

在执行时,正在打印输出“ m”,它是一张地图。问题是如何将这个map m作为HashMap返回到Java方法?谢谢

0 个答案:

没有答案