Android 使用Google Place API的AutoCompleteTextView不起作用
我必须使用Google Places API创建一个Android 使用Google Place API的AutoCompleteTextView不起作用,android,autocompletetextview,google-places-api,Android,Autocompletetextview,Google Places Api,我必须使用Google Places API创建一个AutoCompleteTextView。我没有得到任何错误,也无法得到任何建议,如谷歌搜索框 请建议如何做这件事,或者我哪里做错了 final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item); AutoCompleteTextView textView = (AutoCompleteTextVi
AutoCompleteTextView
。我没有得到任何错误,也无法得到任何建议,如谷歌搜索框
请建议如何做这件事,或者我哪里做错了
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) {
}
});
final ArrayAdapter adapter=新的ArrayAdapter(此,R.layout.list_项);
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(true);
setAdapter(适配器);
addTextChangedListener(新的TextWatcher(){
public void onTextChanged(字符序列、int start、int before、int count){
如果(计数%3==1){
适配器。清除();
试一试{
URL googlePlaces=新URL(
//编码(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=“”);
URLConnection tc=googlePlaces.openConnection();
Log.d(“GottaGo”,URLEncoder.encode(s.toString());
BufferedReader in=新的BufferedReader(新的InputStreamReader(
tc.getInputStream());
弦线;
StringBuffer sb=新的StringBuffer();
而((line=in.readLine())!=null){
某人附加(行);
}
JSONObject预测=新的JSONObject(sb.toString());
JSONArray ja=新的JSONArray(predictions.getString(“predictions”);
对于(int i=0;i
如果您创建了一条LogCat消息,比如GottaGo的消息,其中包含了来自Places Api的一些预期结果,这可以帮助您检查数据以查看问题所在
https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ URLEncoder.encode(s.toString(), "UTF-8") +"&types=geocode&language=fr&sensor=true&key=<getyourowndamnkey>
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=“+URLEncoder.encode(s.toString(),“UTF-8”)+”&types=geocode&language=fr&sensor=true&key=
这是您试图通过URL连接到的链接。你真的把你的代码放进去了吗,或者在这个例子中你只是把默认的谷歌代码放进去了?如果是这样,那就是为什么你不能下载它
尝试将链接放入浏览器中,它仍会显示数据,并会告诉您是否有错误。如果您创建了一条LogCat消息,如GottaGo的消息,其中包含来自Places Api的一些预期结果,这将帮助您检查数据,以查看问题所在
https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ URLEncoder.encode(s.toString(), "UTF-8") +"&types=geocode&language=fr&sensor=true&key=<getyourowndamnkey>
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=“+URLEncoder.encode(s.toString(),“UTF-8”)+”&types=geocode&language=fr&sensor=true&key=
这是您试图通过URL连接到的链接。你真的把你的代码放进去了吗,或者在这个例子中你只是把默认的谷歌代码放进去了?如果是这样,那就是为什么你不能下载它
尝试将链接放入浏览器中,它仍会显示数据,并会告诉您是否有错误。只需将s.toString更改为args[0]:)只需将s.toString更改为args[0]:)