我的情景
我正在构建一个Android应用程序,我使用谷歌地图。
我正在使用API,以Json格式向我提供谷歌位置名称 我在我的适配器上添加了该名称,显示了该位置的提示。 我在应用程序中实现了Place AutoComplete以便给我 当我输入" I"时,印度,印多等的自动建议。
问题陈述
我的Json的数组列表返回NULL,但它应该返回值列表 有自动建议。
这是我的代码
whitebord.setThreshold(0);
names=new ArrayList<String>();
whitebord.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
search_text= whitebord.getText().toString().split(",");
url="https://maps.googleapis.com/maps/api/place/autocomplete/json?
input="+search_text[0]+"&sensor=true&key="+browserKey;
if(search_text.length<=1){
names=new ArrayList<String>();
Log.d("URL",url);
paserdata parse=new paserdata();
parse.execute();
}
}
});
public class paserdata extends AsyncTask<Void, Integer, Void>{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
JSONParser jParser = new JSONParser();
// getting JSON string from URL
json = jParser.getJSONFromUrl(url.toString());
if(json !=null)
{
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_RESULT);
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
String description = c.getString("description");
Log.d("description", description);
names.add(description);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
adp = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, names) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}
};
Toast.makeText(getApplicationContext(), result+"",
Toast.LENGTH_LONG).show();
whitebord.setAdapter(adp);
}
}
答案 0 :(得分:0)
在您的代码中,您构建了一个请求网址,但您尚未使用任何HttpURLConnection
来发出API请求。此外,您需要确保在您的Android清单文件中添加了互联网连接权限。
答案 1 :(得分:0)
您的代码是正确的,但是如果您未将帐户注册为结算帐户,则将无法使用api。看看:https://console.cloud.google.com/billing?pli=1