使用淘宝api,根据ip获取所在地等,
错误信息
net.sf.json.JSONException: A JSONObject text must begin with '{' at character 0 of
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499)
at com.eypg.util.IPUtil.getInfoByIp(IPUtil.java:105)
try {
Proxy httproxy = (Proxy)ApplicationContextListener.context.getBean(Proxy.class);
String result = httproxy.get("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip, null);
JSONObject obj = JSONObject.fromObject(result); // 该行报错
if (obj.getInt("code") == 0) {
......
}
}
catch (HttpException localHttpException) {
}
获取ip的方法如下:
public static String getIpAddr(HttpServletRequest request)
{
String ip = request.getHeader("x-forwarded-for");
if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
ip = request.getHeader("Proxy-Client-IP");
}
if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
ip = request.getRemoteAddr();
}
if (ip.equals("0:0:0:0:0:0:0:1")) {
ip = "本地";
}
if (ip.split(",").length > 1) {
ip = ip.split(",")[0];
}
return ip;
}
result是不是合法的json数据,它是不是{}包括的,有没有别的字符?