我使用following this tutorial的常规自动填充edittext在我的应用中实现了地点api,但是当我在手机上输入文本时,没有返回搜索结果。既没有抛出异常也没有获得搜索结果。我在开发者控制台中启用了places api,并尝试了服务器和android api密钥。有人能指出我的问题吗? 经过调试,我了解到textview正在获取前2个字母并将其传递给数组适配器类并且不返回任何内容。我该如何改变这种行为?
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MY-API-KEY-HERE" />
public static ArrayList autocomplete(String input) {
ArrayList resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&components=country:us");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException mue) {
//Error processing Places API URL
Log.e("TAG", mue.toString());
mue.printStackTrace();
return resultList;
} catch (IOException ioe) {
//Error cannecting to Places API
Log.e("TAG", ioe.toString());
ioe.printStackTrace();
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
//System.out.println(predsJsonArray.getJSONObject(i).getString("description"));
//System.out.println("============================================================");
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException je) {
//cannot process JSON results
Log.e("TAG", je.toString());
je.printStackTrace();
} catch (Exception exe){
Log.e("TAG", exe.toString());
exe.printStackTrace();
}
return resultList;
}
这是xml文件中的自动完成文本视图。
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:completionThreshold="5"/>
答案 0 :(得分:0)
事实证明,Google并不喜欢我的旧api密钥,这对我的代码的其他部分工作正常。我生成了一个新的api密钥,一切正常。