url returns 在 android 中通过 httpconnection 查询没有放置自动完成
Places autocomplete url returns nothing on querying through httpconnection in android
我正在使用 google 文档中的以下代码来获取 android 应用程序的自动完成位置 api。此代码在自动完成文本视图中键入时不会在下拉列表中显示任何结果。
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)
代码 returns 157,如何获取输入查询的自动完成功能
您是否在浏览器中获得了预期的响应?当我使用您的值时,我得到 "status" : "REQUEST_DENIED"
,但可能您已经为 server/browser API 键设置了允许的 IP。
如果您想使用提供 GooglePlaceAutoComplete widget, check out Sprockets 的库(我是开发人员)。使用 API 键进行设置后,您可以向布局添加一个有效的 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"/>
我正在使用 google 文档中的以下代码来获取 android 应用程序的自动完成位置 api。此代码在自动完成文本视图中键入时不会在下拉列表中显示任何结果。
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)
代码 returns 157,如何获取输入查询的自动完成功能
您是否在浏览器中获得了预期的响应?当我使用您的值时,我得到 "status" : "REQUEST_DENIED"
,但可能您已经为 server/browser API 键设置了允许的 IP。
如果您想使用提供 GooglePlaceAutoComplete widget, check out Sprockets 的库(我是开发人员)。使用 API 键进行设置后,您可以向布局添加一个有效的 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"/>