gpt4 book ai didi

android - 在 Google Map API 自动完成教程之后需要帮助

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:56 24 4
gpt4 key购买 nike

我正在尝试在我的应用中使用自动完成

经过一段时间的搜索,我找到了以下教程:

http://www.claytical.com/blog/android-dynamic-autocompletion-using-google-places-api

有一个名为 s 的未解析变量。 正在创建 URL:

URLEncoder.encode(s.toString(), "UTF-8") +

我不明白这个变量是从哪里来的。

这里几乎是对教程中的全部代码进行了一些修改。

public class PlacesListSearchActivity extends Activity {

private static final String TAG = "PlacesListActivity";

public ArrayAdapter<String> adapter;
public AutoCompleteTextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_search);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list);
final 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();
GetPlaces task = new GetPlaces();
//now pass the argument in the textview to the task
task.execute(textView.getText().toString());
}
}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

public void afterTextChanged(Editable s) {

}

});
}

class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> {

@Override
// three dots is java for an array of strings
protected ArrayList<String> doInBackground(String... args)
{
Log.d("gottaGo", "doInBackground");

ArrayList<String> predictionsArr = new ArrayList<String>();

try
{
//https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&sensor=true&key=AddYourOwnKeyHere
URL googlePlaces = new URL(
"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" +
URLEncoder.encode(s.toString(), "UTF-8") +
"&types=geocode&language=en&sensor=true&key=" +
getResources().getString(R.string.googleAPIKey));

URLConnection tc = googlePlaces.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));

String line;
StringBuffer sb = new StringBuffer();
//take Google's legible JSON and turn it into one big string.
while ((line = in.readLine()) != null) {
sb.append(line);
}
//turn that string into a JSON object
JSONObject predictions = new JSONObject(sb.toString());
//now get the JSON array that's inside that object
JSONArray ja = new JSONArray(predictions.getString("predictions"));

for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
//add each entry to our array
predictionsArr.add(jo.getString("description"));
}
} catch (IOException e)
{
Log.e("YourApp", "GetPlaces : doInBackground", e);
} catch (JSONException e)
{
Log.e("YourApp", "GetPlaces : doInBackground", e);
}

return predictionsArr;
}

//then our post

@Override
protected void onPostExecute(ArrayList<String> result){

Log.d("YourApp", "onPostExecute : " + result.size());
//update the adapter
adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.item_list);
adapter.setNotifyOnChange(true);
//attach the adapter to textview
textView.setAdapter(adapter);

for (String string : result) {
Log.d("YourApp", "onPostExecute : result = " + string);
adapter.add(string);
adapter.notifyDataSetChanged();
}

Log.d("YourApp", "onPostExecute : autoCompleteAdapter" + adapter.getCount());

}

}

}

好的,我回答了我自己的问题。请参阅下面的答案!

最佳答案

好的,我明白了。上面的 URL 应该这样计算:

            URL googlePlaces = new URL(
"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" +
URLEncoder.encode(args[0], "UTF-8") +
"&types=geocode&language=en&sensor=true&key=" +
getResources().getString(R.string.googleAPIKey));

关于android - 在 Google Map API 自动完成教程之后需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11169758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com