AutoCompleteTextView 使用 Google Place API 不起作用
我必须使用 Google Places API 创建一个 AutoCompleteTextView
。我已经尝试了以下代码,但它不起作用。我没有收到任何错误,也无法获得任何建议,例如 Google 搜索框。
请建议如何执行此操作或我哪里出错了。
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(true);
textView.setAdapter(adapter);
textView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count%3 == 1) {
adapter.clear();
try {
URL googlePlaces = new URL(
// URLEncoder.encode(url,"UTF-8");
"https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ URLEncoder.encode(s.toString(), "UTF-8") +"&types=geocode&language=fr&sensor=true&key=<getyourowndamnkey>");
URLConnection tc = googlePlaces.openConnection();
Log.d("GottaGo", URLEncoder.encode(s.toString()));
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
}
JSONObject predictions = new JSONObject(sb.toString());
JSONArray ja = new JSONArray(predictions.getString("predictions"));
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
adapter.add(jo.getString("description"));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
}
});
I have to create an AutoCompleteTextView
using Google Places API.I have tried the following code, but it is not working. I am not getting any error, and am unable to get any suggestion like Google search box.
Please suggest how to do this or where I am going wrong.
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(true);
textView.setAdapter(adapter);
textView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count%3 == 1) {
adapter.clear();
try {
URL googlePlaces = new URL(
// URLEncoder.encode(url,"UTF-8");
"https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ URLEncoder.encode(s.toString(), "UTF-8") +"&types=geocode&language=fr&sensor=true&key=<getyourowndamnkey>");
URLConnection tc = googlePlaces.openConnection();
Log.d("GottaGo", URLEncoder.encode(s.toString()));
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
}
JSONObject predictions = new JSONObject(sb.toString());
JSONArray ja = new JSONArray(predictions.getString("predictions"));
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
adapter.add(jo.getString("description"));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您创建一条 LogCat 消息,就像您为 GottaGo 创建的消息一样,其中包含来自 Places Api 的一些预期结果,这可以帮助您检查数据以了解问题所在。
这是您尝试通过 URL 连接到的链接。您是否实际输入了代码,或者只是在本示例中放置了默认的谷歌代码?如果是这样,这就是您无法下载的原因。
尝试将链接放入浏览器,它应该仍然显示数据,并且它会告诉您是否有错误。
If you create a LogCat message, like the one you have for GottaGo which includes a few expected results from the Places Api, this could help you check the data to see where the problem is at.
This is the link you are trying to URL Connect to. Have you actually put in your code, or did you just place the default google one in there for this example? If so, that is why you can't download it.
Try putting the link into your browser, it should still display data, and it will tell you if there is an error.
只需将 s.toString 更改为 args[0] :)
Just change the s.toString to args[0] :)