错误描述
给以企业做的app中内嵌了通过gps地址信息转换为实际的地址的功能,使用了百度地图的Geocoding 功能,从上周三开始,陆续有用户反馈,使用定位出现无法获取地址信息的错误,一开始小范围,还以为是用户自己的问题,没有在意,但是到了周四的时候,就开始大面积的爆发了;
解决方法
查找下是不是百度api发生变更
因为近期android app没有做任何更新,所以怀疑是不是baidu的api发生了修改,但是看了下文档,也没有发生变更,而且按照baidu之前的习惯应该是可以向下兼容的;
看下是不是key过期了
登陆百度map的api管理控制台,看了下key没有过期,并且通过代码也是可以访问的;
更换了key之后,调用后,问题依旧;
检查android代码
1、检查了下调用的代码,发现是使用httpget方式进行获取,就考虑换个post方法看看是不是能够解决问题;
2、替换为httppost方式之后,问题得到解决,可以确认是baidu修改了该api的获取方式,拒绝了get方式活着拒绝了android代码里面httpget方式;
public static String getUrlData(String url)
throws Exception {
int returnCode = 0;
url = checkURL(url);
HttpGet get = new HttpGet(url);
String strResult = “”;
HttpParams httpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(httpParams, 3000);get.setHeader(“text/html;charset”, “utf-8″);
ConnManagerParams.setTimeout(httpParams, 3000);
HttpConnectionParams.setConnectionTimeout(httpParams, 4000);
HttpConnectionParams.setSoTimeout(httpParams, 8000);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme(“http”, PlainSocketFactory
.getSocketFactory(), 80));/* HttpClient */
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
httpParams, schReg);
HttpClient httpClient = new DefaultHttpClient(conMgr, httpParams);//HttpClient httpClient =new DefaultHttpClient();
try{
HttpResponse response = httpClient.execute(get);
//HttpResponse response = new DefaultHttpClient().execute(get);
returnCode = response.getStatusLine().getStatusCode();
if(returnCode == 200){
strResult =EntityUtils.toString(response.getEntity(),”UTF-8″);//String result = EntityUtils.toString(response.getEntity(),”UTF-8″);
KonkaLog.i(response.getAllHeaders().toString());
for(int i=0;i<response.getAllHeaders().length;i++){
KonkaLog.i(response.getAllHeaders()[i].getName(),response.getAllHeaders()[i].getValue());
}
com.easybiz.util.KonkaLog.i(“postUrlData”, strResult );return strResult;
}else{
com.easybiz.util.KonkaLog.i(“postUrlData”, returnCode + “”);
return “”+returnCode;
}
}catch(Exception e){
return “发生未知错误”;
}
}
public static String postUrlData(String url)
throws Exception {
int returnCode = 0;
url = checkURL(url);
HttpPost post = new HttpPost(url);
String strResult = “”;
HttpClient httpClient = new DefaultHttpClient();HttpResponse response = httpClient.execute(post);
returnCode = response.getStatusLine().getStatusCode();
if(returnCode == 200){
strResult =EntityUtils.toString(response.getEntity());
com.easybiz.util.KonkaLog.i(“postUrlData”, strResult );
ErrorCode = 200;
return strResult;}else{
com.easybiz.util.KonkaLog.i(“postUrlData”, returnCode + “”);
ErrorDescript = returnCode+”";
return “”+returnCode;
}}
/**
* httppost的公用类
* @param url 地址
* @param params 参数
* @return 返回string类型
* @throws Exception
*/
public static String postUrlData(String url, ArrayList<NameValuePair> params)
throws Exception {
int returnCode = 0;
url = checkURL(url);
HttpPost post = new HttpPost(url);String strResult = “”;
com.easybiz.util.KonkaLog.i(“postUrlData”, params.toString() );
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpParams httpParams = new BasicHttpParams();ConnManagerParams.setTimeout(httpParams, 3000);
ConnManagerParams.setTimeout(httpParams, 3000);
HttpConnectionParams.setConnectionTimeout(httpParams, 120000);
HttpConnectionParams.setSoTimeout(httpParams, 120000);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme(“http”, PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
httpParams, schReg);
HttpClient httpClient = new DefaultHttpClient(conMgr, httpParams);//HttpClient httpClient =new DefaultHttpClient();
try{HttpResponse response = httpClient.execute(post);
returnCode = response.getStatusLine().getStatusCode();
strResult =EntityUtils.toString(response.getEntity());
if(returnCode == 200){com.easybiz.util.KonkaLog.i(“postUrlData”, strResult );
return strResult;}else{
com.easybiz.util.KonkaLog.i(“postUrlData”, returnCode + “”);
ErrorDescript = “登录失败”;
return “”+returnCode;
}
}catch(Exception e){
com.easybiz.util.KonkaLog.i(“postUrlData”, strResult );
e.printStackTrace();
ErrorDescript = “网络连接失败,请检查WIFI或数据连接是否正常”;
return “网络连接失败,请检查WIFI或数据连接是否正常”;
}
}
method_url = method_url +”?output=json” + “&ak=Dlwp0PmEQQKyFBBntHGSLljM” + “&location=” + loc.getLatitude()+”,”+loc.getLongitude();
strresult = HttpComm.getUrlData(method_url));//公用方法调用见上面将此处修改为
strresult = HttpComm.postUrlData(method_url));//公用方法调用见上面
3、但是修改为post方法后,发现会出现10次调用,有3次会出现失败,再次检查代码,发现还是实用得得httppost的方式不对,换成标准方式,问题得到解决;
method_url = method_url +”?output=json” + “&ak=Dlwp0PmEQQKyFBBntHGSLljM” + “&location=” + loc.getLatitude()+”,”+loc.getLongitude();
strresult = HttpComm.postUrlData(method_url));//公用方法调用见上面将此处修改为
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
BasicNameValuePair baseValuePair = new BasicNameValuePair(“output”,”json”);
params.add(baseValuePair);
baseValuePair = new BasicNameValuePair(“location”,loc.getLatitude()+”,”+loc.getLongitude());
params.add(baseValuePair);
baseValuePair = new BasicNameValuePair(“ak”,”Dlwp0PmEQQKyFBBntHGSLljM”);
params.add(baseValuePair);strresult = HttpComm.postUrlData(method_url,params );
====================================================
欢迎关注我的微信号@it51share
欢迎加入:179203853(安徽微信开发群)
====================================================
与本文相关的文章
本文出自 “无名程序员” 博客,请务必保留此出处http://wmcxy.blog.51cto.com/2509483/1574842