I've done all solutions that I can search. I enabled APIs that are related to my project and made API keys(browser,android) slowly, and surely but not one is working. According to this that my API key is malformed or missing. Is there a way to check if the value in my manifest is changing or getting null when I run my project?
I solved the problem by using a code that i found from tutorials. hope this help for the people having the same trouble.
I made a PlaceAPI class
public class PlaceAPI{
private static final String TAG = PlaceAPI.class.getSimpleName();
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 = "key here";
public ArrayList<String> autocomplete (String input) {
ArrayList<String> resultList = null;
ArrayList<String> resultListid = 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.trim());
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
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);
}
} catch (MalformedURLException e) {
Log.e(TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Log.d(TAG, jsonResults.toString());
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");//result
// 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"));//geometry
}
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
//GET PLACEID
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObjid = new JSONObject(jsonResults.toString());
JSONArray predsJsonArrayid = jsonObjid.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultListid = new ArrayList<String>(predsJsonArrayid.length());
for (int i = 0; i < predsJsonArrayid.length(); i++) {
resultListid.add(predsJsonArrayid.getJSONObject(i).getString("id"));
}
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
return resultList;
}
}
and made an adapter PlacesAutoCompleteAdapter
class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
ArrayList<String> resultList;
Context mContext;
int mResource;
PlaceAPI mPlaceAPI = new PlaceAPI();
public PlacesAutoCompleteAdapter(Context context, int resource) {
super(context, resource);
mContext = context;
mResource = resource;
}
@Override
public int getCount() {
// Last item will be the footer
return resultList.size();
}
@Override
public String getItem(int position) {
return resultList.get(position);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
resultList = mPlaceAPI.autocomplete(constraint.toString());
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;
}
}
then using the adapter in autocomplete:
autocomplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);
autocomplete.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.autocomplete_list_item));
autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get data associated with the specified position
// in the list (AdapterView)
place = (String) parent.getItemAtPosition(position);
//Toast.makeText(this, description, Toast.LENGTH_SHORT).show();
}
});