js根据IP地址获取当前的省市

3.阿里巴巴提供了接口,但不支持js跨域请求回调,我们可以在java代码中请求,返回参数:

//工具类
public class ResponseUtil {

    public static void write(HttpServletResponse response,Object o)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println(o.toString());
        out.flush();
        out.close();
    }

}



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.Callable;


public class IPLocationTask implements Callable<Map<String, String>>  {


	private String id;
	private String uri;
	private String type;
	
	public IPLocationTask(String id,String type, String url) {
		super();
		this.id = id;
		this.uri = url;
		this.type = type;
	}

	@Override
	public Map<String, String> call() throws Exception {
		URL url=new URL(uri);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        conn.setReadTimeout(10*1000);  
        conn.setRequestMethod("GET");  
          
        InputStreamReader  inStream = new InputStreamReader(conn.getInputStream(),"UTF-8");  
        BufferedReader buffer=new BufferedReader(inStream);  
           
        String inputline=null;
        String info="";
		while((inputline=buffer.readLine())!=null){  
            info+=inputline;  
        }  
		System.out.println(type+"=="+info);
		return IPLocationParseUtil.parse(type, info);
	}
	
}
	private String id;
	private String uri;
	private String type;
	
	public IPLocationTask(String id,String type, String url) {
		super();
		this.id = id;
		this.uri = url;
		this.type = type;
	}

	@Override
	public Map<String, String> call() throws Exception {
		URL url=new URL(uri);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        conn.setReadTimeout(10*1000);  
        conn.setRequestMethod("GET");  
          
        InputStreamReader  inStream = new InputStreamReader(conn.getInputStream(),"UTF-8");  
        BufferedReader buffer=new BufferedReader(inStream);  
           
        String inputline=null;
        String info="";
		while((inputline=buffer.readLine())!=null){  
            info+=inputline;  
        }  
		System.out.println(type+"=="+info);
		return IPLocationParseUtil.parse(type, info);
	}
	
}

 

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

public class IPLocationParseUtil {
    /**
     * 解析 各ip查询提供 服务的返回结果
     * @param type
     * @param result
     * @return
     */
    public static Map<String, String> parse(String type,String result){
        
        if(StringUtils.isEmpty(result)){
            Map<String, String> error=new HashMap<String, String>();
            error.put("status", "error");
            error.put("msg", "exception");
            return error;
        }
        
        Map<String, String> map=new HashMap<String, String>();
        map.put("type", type);
        switch (type) {

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值