gpt4 book ai didi

java - 在 android 中通过 httpconnection 查询时,位置自动完成 url 不返回任何内容

转载 作者:行者123 更新时间:2023-12-01 11:44:17 26 4
gpt4 key购买 nike

我正在使用 google 文档中的以下代码来获取 android 应用程序的自动完成位置 api。在自动完成 TextView 中键入时,此代码不会在下拉列表中显示任何结果。

class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> resultList;
private static final String LOG_TAG = "Buzz";

private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";

private static final String API_KEY = "AIzaSyCPrebeTr0lmiyjSwc1fdK82ZXLsmleSKc";

private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;

HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&components=country:in");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
sb.append("&types=(cities)&language=iw");

Log.v(LOG_TAG,"URL for places: "+sb.toString());

URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();

if (!(conn instanceof HttpURLConnection))
{
Log.v(LOG_TAG,"Connection not created ");
}

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);
Log.v(LOG_TAG,"Value in input stream buffer: "+read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
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");

Log.v(LOG_TAG,"Predictions for given query: "+predsJsonArray.length());

// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));

Log.v(LOG_TAG,"Results for the given url: "+predsJsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}

return resultList;
}

public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}

@Override
public int getCount() {
return resultList.size();
}

@Override
public String getItem(int index) {
return resultList.get(index);
}

@Override
public Filter getFilter() {

Filter filter = new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults filterResults = new FilterResults();
if (constraint != null)
{
Log.v(LOG_TAG,"Search string: "+constraint.toString());
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());

// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return filter;
}

}

in.read(buff)代码返回157,如何获取输入查询的自动完成

最佳答案

您在浏览器中得到预期的响应吗?我得到"status" : "REQUEST_DENIED"当我使用你的值时,但可能你已经为服务器/浏览器 API key 设置了允许的 IP。

如果您想使用提供 GooglePlaceAutoComplete 的库小部件,查看 Sprockets (我是开发商)。使用 API key 进行设置后,您可以将可用的 Places API 自动完成功能添加到您的布局中,如下所示:

<net.sf.sprockets.widget.GooglePlaceAutoComplete
xmlns:sprockets="http://schemas.android.com/apk/res-auto"
android:id="@+id/place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
sprockets:types="(cities)"
sprockets:countries="IN"
sprockets:language="iw"/>

关于java - 在 android 中通过 httpconnection 查询时,位置自动完成 url 不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29279938/

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