基本信息
源码名称:android 百度地图搜索自定义周边 例子源码下载
源码大小:2.27M
文件格式:.zip
开发语言:Java
更新时间:2015-05-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import com.baidu.lbs.duanzu.DemoApplication;
import com.baidu.lbs.duanzu.MainActivity;
/**
* 百度云检索使用类
* @author Lu.Jian
*
*/
public class LBSCloudSearch {
private static String mTAG = "NetWorkManager";
//百度云检索API URI
private static final String SEARCH_URI_NEARBY = "http://api.map.baidu.com/geosearch/v2/nearby?";
private static final String SEARCH_URI_LOCAL = "http://api.map.baidu.com/geosearch/v2/local?";
public static final int SEARCH_TYPE_NEARBY = 1;
public static final int SEARCH_TYPE_LOCAL = 2;
private static int currSearchType = 0;
//云检索公钥
private static String ak = "A4749739227af1618f7b0d1b588c0e85";
private static String geotable_id = "30960";
private static int TIME_OUT = 12000;
private static int retry = 3;
private static boolean IsBusy = false;
/**
* 云检索访问
* @param filterParams 访问参数,key为filter时特殊处理。
* @param handler 数据回调Handler
* @param networkType 手机联网类型
* @return
*/
public static boolean request(final int searchType,final HashMap<String, String> filterParams,final Handler handler, final String networkType) {
if (IsBusy || filterParams == null)
return false;
IsBusy = true;
new Thread() {
public void run() {
int count = retry;
while (count > 0){
try {
//根据过滤选项拼接请求URL
String requestURL = "";
if(searchType == -1){
//沿用上次搜索保存的search type
if(currSearchType == SEARCH_TYPE_NEARBY){
requestURL = SEARCH_URI_NEARBY;
}else if(currSearchType == SEARCH_TYPE_LOCAL){
requestURL = SEARCH_URI_LOCAL;
}
}else{
if(searchType == SEARCH_TYPE_NEARBY){
requestURL = SEARCH_URI_NEARBY;
}else if(searchType == SEARCH_TYPE_LOCAL){
requestURL = SEARCH_URI_LOCAL;
}
currSearchType = searchType;
}
requestURL = requestURL "&"
"ak=" ak
"&geotable_id=" geotable_id;
String filter = null;
Iterator iter = filterParams.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = entry.getKey().toString();
String value = entry.getValue().toString();
if(key.equals("filter")){
filter = value;
}else{
if(key.equals("region") && currSearchType == SEARCH_TYPE_NEARBY){
continue;
}
requestURL = requestURL "&" key "=" value;
}
}
if(filter != null && !filter.equals("")){
//substring(3) 为了去掉"|" 的encode "%7C"
requestURL = requestURL "&filter=" filter.substring(3);
}
Log.d("DuanZuLog", "request url:" requestURL);
HttpGet httpRequest = new HttpGet(requestURL);
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT);
httpclient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, TIME_OUT);
HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);
if(networkType.equals("cmwap")){
HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
}else if(networkType.equals("ctwap")){
HttpHost proxy = new HttpHost("10.0.0.200", 80, "http");
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
}
HttpResponse httpResponse = httpclient.execute(httpRequest);
int status = httpResponse.getStatusLine().getStatusCode();
if ( status == HttpStatus.SC_OK ) {
String result = EntityUtils.toString(httpResponse
.getEntity(), "utf-8");
Header a = httpResponse.getEntity().getContentType();
Message msgTmp = handler.obtainMessage(MainActivity.MSG_NET_SUCC);
msgTmp.obj = result;
msgTmp.sendToTarget();
break;
} else {
httpRequest.abort();
Message msgTmp = handler.obtainMessage(MainActivity.MSG_NET_STATUS_ERROR);
msgTmp.obj = "HttpStatus error";
msgTmp.sendToTarget();
}
} catch (Exception e) {
Log.e("DuanZuLog", "网络异常,请检查网络后重试!");
e.printStackTrace();
}
count--;
}
if ( count <= 0 && handler != null){
Message msgTmp = handler.obtainMessage(MainActivity.MSG_NET_TIMEOUT);
msgTmp.sendToTarget();
}
IsBusy = false;
}
}.start();
return true;
}
}