httpClient 反查 某一个地址的坐标

package com.yidiyiwei.erp.base.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import com.yidiyiwei.framework.jackson.JacksonMapper;
/**
 * @author liushuai
 * @date 2015/07/14 11:04
 * @desc 地址解析
 * 
 * **/
public class LocationParse {
private static String defaultEncodeStr="utf-8";
public static String getLogLatByLocationStr(String locationStr){
HttpClient httpclient = HttpClients.createDefault();
//HttpRequest hur=new 
//Map<String , String> map = new HashMap<String, String>();
//map.put("params", TripleDES.encode(new JacksonMapper().writeValueAsString(params)));
    //2、参数集合转换成NameValuePair集合
//    List<NameValuePair> list=new ArrayList<NameValuePair>();
//        for(Map.Entry<String, String> entry: map.entrySet()){
//            list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
//        }
        
        //实现将请求中的参数封装到请求参数中,请求体中
//        UrlEncodedFormEntity  entity=new UrlEncodedFormEntity(list,defaultEncodeStr);
        //使用post方式提交
//httpPost=new HttpPost(basePath + serviceCode);
String county="";
String getUrl="http://api.map.baidu.com/geocoder?address="+locationStr+"&qq-pf-to=pcqq.c2c";
HttpGet httpGet=new HttpGet(getUrl);
//        httpPost.setEntity(entity);
        //指定post方式提交数据
HttpClient client =HttpClients.createDefault() ;
         
        HttpResponse httpResponse;
try {
httpResponse = client.execute(httpGet);
 String responseString = "";
        if(httpResponse.getStatusLine().getStatusCode() == 200){
        responseString = changeInputStream(httpResponse.getEntity().getContent(),"utf-8");
        }
        System.out.println(responseString);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       
//@SuppressWarnings("unchecked")
//Map<String, Object> result= new JacksonMapper().readValue(responseString,Map.class);
//System.out.println(result.get("status"));
//System.out.println(TripleDES.decode(String.valueOf(result.get("result"))));
return "";
}
    /**
     * 把数据流转换成字符串
     * @param inputStream
     * @param encode
     * @return
     */
    private static String changeInputStream(InputStream inputStream,
            String encode) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int len = 0;
        byte[] date = new byte[1024];
        String result = "";
        try {
 
            while ((len = inputStream.read(date)) != -1) {
                outputStream.write(date, 0, len);
            }
            result = new String(outputStream.toByteArray(), encode);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static void main(String[] args) {
    LocationParse.getLogLatByLocationStr("上地九街9号");
}
    
}

你可能感兴趣的:(httpclient,地址反查坐标)