2011-11-24 7 views
1

Google Places API를 사용하여 AutoCompleteTextView을 만들어야합니다. 다음 코드를 시도했지만 작동하지 않습니다. 어떤 오류도 발생하지 않고 Google 검색 상자와 같은 제안을 얻을 수 없습니다.AutoCompleteTextView Google Place API 사용이 작동하지 않습니다.

이 작업을 수행하는 방법이나 내가 잘못하고있는 곳을 제안하십시오. 당신이 로그 캣 메시지를 작성하는 경우

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) { 

} 
}); 

답변

1

는 장소 API의 몇 가지 예상되는 결과를 포함 GottaGo에 대한 당신이 가지고있는 하나처럼, 이것은 당신이 문제에 여기서 볼 수있는 데이터를 확인하는 데 도움 수 있습니다.

https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ URLEncoder.encode(s.toString(), "UTF-8") +"&types=geocode&language=fr&sensor=true&key=<getyourowndamnkey> 

URL 연결을 시도하는 링크입니다. 실제로 코드를 작성 했습니까? 아니면이 예제에서 기본 Google을 하나만 배치 했습니까? 그렇다면 다운로드 할 수없는 것입니다.

브라우저에 링크를 넣어보세요. 그래도 데이터가 표시되어야하며 오류가 있으면 알려줍니다.

0

s.toString을 args [0]로 변경하십시오.

관련 문제