问题描述
提示:百度lbs周边查询结果为空 或者 suggestionResult返回结果始终为空
查询poi参考代码:
private SuggestionSearch mSuggestionSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView poi = findViewById(R.id.poi_text_view);
mSuggestionSearch = SuggestionSearch.newInstance();
OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
@Override
public void onGetSuggestionResult(SuggestionResult suggestionResult) {
//处理sug检索结果
if (suggestionResult == null || suggestionResult.getAllSuggestions() == null) {
Log.d("MainActivity", "暂无查询结果");
return;
}
ArrayList<String> mLoadMoreSuggest = new ArrayList<String>();
for (SuggestionResult.SuggestionInfo info : suggestionResult.getAllSuggestions()) {
if (info.key != null) {
mLoadMoreSuggest.add(info.key);
}
}
poi.setText(mLoadMoreSuggest.toString());
}
};
mSuggestionSearch.setOnGetSuggestionResultListener(listener);
mSuggestionSearch.requestSuggestion(new SuggestionSearchOption()
.city("宜宾")
.keyword("学院")
.citylimit(false));
}
原因分析:
SDK 校验失败
注意:申请百度Key时,包名注意写完整参考方法(引用来源百度LBS文档)
Android Studio可以通过applicationId配置包名,如果配置了build.gradle文件,包名应该以applicaionId为准,防止build.gradle中的applicationId与 AndroidMainfest.xml中的包名不同,导致AK鉴权失败。
解决方案:
注意:重新申请百度的Key,包名填写正确!基本应该没有问题
附加SDK校验参考代码:
1.创建类:
public class SDKReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {
Log.d("MainActivity SDk ", "onReceive: 验证失败");
Toast.makeText(context.getApplicationContext(), "KEY 验证失败!", Toast.LENGTH_SHORT).show();
} else if (action.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK)) {
Log.d("MainActivity SDk ", "onReceive: 验证成功");
Toast.makeText(context.getApplicationContext(), "KEY 验证成功!", Toast.LENGTH_SHORT).show();
}
}
}
2.在活动中发送广播
/**动态注册广播*/
IntentFilter iFilter = new IntentFilter();
iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK);
SDKReceiver mReceiver = new SDKReceiver();
registerReceiver(mReceiver, iFilter);
2021年10月9日