百度地图API 云存储·LBS.云 跨域问题解决方法——通过java.net.URLConnection发送HTTP请求

百度地图API 云存储·LBS.云 跨域问题解决方法——通过java.net.URLConnection发送HTTP请求

运用原生Java Api发送简单的Get请求、Post请求

HTTP请求一般为两种,一种是GET请求,一种是POST请求。

使用Java发送这两种请求步骤如下:

  1. 通过统一资源定位器(java.net.URL)获取连接器(java.net.URLConnection)
  2. 设置请求的参数
  3. 发送请求
  4. 以输入流的形式获取返回内容
  5. 关闭输入流

根据上面步骤封装成HttpRequestor.java 工具类 网上参考代码很多,下面为调试并优化过的代码,修复了乱码等问题


package com.xeonmic.lbs.util;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;


public class HttpRequestor {

    private String charset = "utf-8";
    private Integer connectTimeout = null;
    private Integer socketTimeout = null;
    private String proxyHost = null;
    private Integer proxyPort = null;

    /**
     * Do GET request
     * @param url
     * @return
     * @throws Exception
     * @throws IOException
     */
    public String doGet(String url) throws Exception {

        URL localURL = new URL(url);

        URLConnection connection = openConnection(localURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;

        httpURLConnection.setRequestProperty("Accept-Charset", charset);
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
       // BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }

        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            reader = new BufferedReader(inputStreamReader);

            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }

        } finally {

            if (reader != null) {
                reader.close();
            }

            if (inputStreamReader != null) {
                inputStreamReader.close();
            }

            if (inputStream != null) {
                inputStream.close();
            }

        }

        return resultBuffer.toString();
    }

    /**
     * Do POST request
     * @param url
     * @param parameterMap
     * @return
     * @throws Exception 
     */
    public String doPost(String url, Map parameterMap) throws Exception {

        /* Translate parameter map to parameter date string */
        StringBuffer parameterBuffer = new StringBuffer();
        if (parameterMap != null) {
            Iterator iterator = parameterMap.keySet().iterator();
            String key = null;
            String value = null;
            while (iterator.hasNext()) {
                key = iterator.next();
                if (parameterMap.get(key) != null) {
                    value =parameterMap.get(key).toString();
                } else {
                    value = "";
                }

                parameterBuffer.append(key).append("=").append(value);
                if (iterator.hasNext()) {
                    parameterBuffer.append("&");
                }
            }
        }

        System.out.println("POST parameter : " + parameterBuffer.toString());

        URL localURL = new URL(url);

        URLConnection connection = openConnection(localURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;

        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept-Charset", charset);
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterBuffer.length()));

        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;

        try {
            outputStream = httpURLConnection.getOutputStream();
            outputStreamWriter = new OutputStreamWriter(outputStream);

            outputStreamWriter.write(parameterBuffer.toString());
            outputStreamWriter.flush();

            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }

            inputStream = httpURLConnection.getInputStream();
           // inputStreamReader = new InputStreamReader(inputStream);
            inputStreamReader = new InputStreamReader(inputStream,"utf-8");
            reader = new BufferedReader(inputStreamReader);

            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }

        } finally {

            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }

            if (outputStream != null) {
                outputStream.close();
            }

            if (reader != null) {
                reader.close();
            }

            if (inputStreamReader != null) {
                inputStreamReader.close();
            }

            if (inputStream != null) {
                inputStream.close();
            }

        }

        return resultBuffer.toString();
    }

    private URLConnection openConnection(URL localURL) throws IOException {
        URLConnection connection;
        if (proxyHost != null && proxyPort != null) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            connection = localURL.openConnection(proxy);
        } else {
            connection = localURL.openConnection();
        }
        return connection;
    }

    /**
     * Render request according setting
     * @param request
     */
    private void renderRequest(URLConnection connection) {

        if (connectTimeout != null) {
            connection.setConnectTimeout(connectTimeout);
        }

        if (socketTimeout != null) {
            connection.setReadTimeout(socketTimeout);
        }

    }

    /*
     * Getter & Setter
     */
    public Integer getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public Integer getSocketTimeout() {
        return socketTimeout;
    }

    public void setSocketTimeout(Integer socketTimeout) {
        this.socketTimeout = socketTimeout;
    }

    public String getProxyHost() {
        return proxyHost;
    }

    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }

    public Integer getProxyPort() {
        return proxyPort;
    }

    public void setProxyPort(Integer proxyPort) {
        this.proxyPort = proxyPort;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

}

编写一个测试类分别测试GET和POST方法

package com.xeonmic.text;

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

    import com.xeonmic.lbs.util.HttpRequestor;
/**
 * @author XeonM
 * @version 3.0
 */
public class httpTest {
    public static void main(String[] args) throws Exception {
        /* Post Request */
        Map dataMap = new HashMap();
        dataMap.put("ak", "YourKey");
        dataMap.put("geotable_id", "YourTableId");
        dataMap.put("latitude", 28.5555);
        dataMap.put("longitude", 115.555);
        dataMap.put("coord_type", 1);
        dataMap.put("title", "title");         
        System.out.println(new HttpRequestor().doPost("http://api.map.baidu.com/geodata/v3/poi/create", dataMap));
        /* Get Request */
        System.out.println(new HttpRequestor().doGet("http://api.map.baidu.com/geodata/v3/poi/list?geotable_id=YourTableId&ak=YourKey"));       
    }
}

运行结果:


POST parameter : ak=YourKey&coord_type=1&title=title&geotable_id=YourTableId&longitude=115.555&latitude=28.5555
{"status":0,"id":1536459136,"message":"\u6210\u529f"}
{"status":0,"size":3,"total":3,"pois":[{"title":"\u963f\u8428\u5fb7\u554a","location":[115.56640247684,28.558452229427],"city":"\u5b9c\u6625\u5e02","create_time":"2015-06-02 12:25:35","geotable_id":105115,"address":"\u6309\u65f6","province":"\u6c5f\u897f\u7701","district":"\u9ad8\u5b89\u5e02","city_id":278,"id":905313705},{"title":"\u4eca\u5929","location":[115.56640247684,28.558452229427],"city":"\u5b9c\u6625\u5e02","create_time":"2015-06-02 06:40:28","geotable_id":105115,"province":"\u6c5f\u897f\u7701","district":"\u9ad8\u5b89\u5e02","city_id":278,"id":904395000},{"title":"\u4e2d\u6587","location":[115.56640247684,28.558452229427],"city":"\u5b9c\u6625\u5e02","create_time":"2015-06-02 06:17:35","geotable_id":105115,"province":"\u6c5f\u897f\u7701","district":"\u9ad8\u5b89\u5e02","city_id":278,"id":904382413}],"message":"\u6210\u529f"}

status=0 代表成功;测试请将代码中YourKey改为你所申请的密钥 将YourTableId改为你API控制台中对应的表ID 如不清楚请见我的其他文章


附录2:控制服务返回码定义

状态码 定义 注释
0 正常
1 服务器内部错误 该服务响应超时或系统内部错误,请留下联系方式
10 上传内容超过8M Post上传数据不能超过8M
101 AK参数不存在 请求消息没有携带AK参数
102 MCODE参数不存在,mobile类型mcode参数必需 对于Mobile类型的应用请求需要携带mcode参数,该错误码代表服务器没有解析到mcode
200 APP不存在,AK有误请检查再重试 根据请求的ak,找不到对应的APP
201 APP被用户自己禁用,请在控制台解禁
202 APP被管理员删除 恶意APP被管理员删除
203 APP类型错误 当前API控制台支持Server(类型1), Mobile(类型2, 新版控制台区分为Mobile_Android(类型21)及Mobile_IPhone(类型22))及Browser(类型3),除此之外其他类型认为是APP类型错误
210 APP IP校验失败 在申请SERVER类型应用的时候选择IP校验,需要填写IP白名单,如果当前请求的IP地址不在IP白名单或者不是0.0.0.0/0就认为IP校验失败
211 APP SN校验失败 SERVER类型APP有两种校验方式IP校验和SN校验,当用户请求的SN和服务端计算出来的SN不相等的时候提示SN校验失败
220 APP Referer校验失败 浏览器类型的APP会校验referer字段是否存且切在referer白名单里面,否则返回该错误码
230 APP Mcode码校验失败 服务器能解析到mcode,但和数据库中不一致,请携带正确的mcode
240 APP 服务被禁用 用户在API控制台中创建或设置某APP的时候禁用了某项服务
250 用户不存在 根据请求的user_id, 数据库中找不到该用户的信息,请携带正确的user_id
251 用户被自己删除 该用户处于未激活状态
252 用户被管理员删除 恶意用户被加入黑名单
260 服务不存在 服务器解析不到用户请求的服务名称
261 服务被禁用 该服务已下线
301 永久配额超限,限制访问 配额超限,如果想增加配额请联系我们
302 天配额超限,限制访问 配额超限,如果想增加配额请联系我们
401 当前并发量已经超过约定并发配额,限制访问 并发控制超限,请控制并发量或联系我们
402 当前并发量已经超过约定并发配额,并且服务总并发量也已经超过设定的总并发配额,限制访问 并发控制超限,请控制并发量或联系我们

你可能感兴趣的:(Java方向,百度地图API方向)