8

当我在我的 Android 应用程序中输入一个空格时,我的下拉菜单中不再有任何结果。我怎样才能解决这个问题?我正在对输入进行编码,所以我不确定它为什么不起作用......

空间前: 在此处输入图像描述

空间之后: 在此处输入图像描述

我查看了https://developers.google.com/places/documentation/autocomplete,但没有找到任何可以帮助我的东西。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.AutoCompleteTextView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    AutoCompleteTextView atvPlaces;
    PlacesTask placesTask;
    ParserTask parserTask;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
    atvPlaces.setThreshold(1);

    atvPlaces.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            placesTask = new PlacesTask();
            placesTask.execute(s.toString());
        }

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

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });
}

/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException{
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try{
        URL url = new URL(strUrl);

        // Creating an http connection to communicate with url
        urlConnection = (HttpURLConnection) url.openConnection();

        // Connecting to url
        urlConnection.connect();

        // Reading data from url
        iStream = urlConnection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

        StringBuffer sb = new StringBuffer();

        String line = "";
        while( ( line = br.readLine()) != null){
            sb.append(line);
        }

        data = sb.toString();

        br.close();

    }catch(Exception e){
        Log.d("Exception while downloading url", e.toString());
    }finally{
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

// Fetches all places from GooglePlaces AutoComplete Web Service
private class PlacesTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... place) {
        // For storing data from web service 
        String data = "";

        // Obtain browser key from https://code.google.com/apis/console
        String key = "key=AIzaSyDcXFSC2I6ZqxQeAbUMFvJKMrA98217H9U";

        String input="";

        try {
            input = "input=" + URLEncoder.encode(place[0], "utf-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

        // place type to be searched
        String types = "types=(regions)";

        // Sensor enabled
        String sensor = "sensor=false";

        // Building the parameters to the web service
        String parameters = input+"&"+types+"&"+sensor+"&"+key;

        // Output format
        String output = "json";

        // Building the url to the web service
        String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;

        try{
            // Fetching the data from we service
            data = downloadUrl(url);
        }catch(Exception e){
            Log.d("Background Task",e.toString());
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        // Creating ParserTask
        parserTask = new ParserTask();

        // Starting Parsing the JSON string returned by Web Service
        parserTask.execute(result);
    }
}
/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{

    JSONObject jObject;

    @Override
    protected List<HashMap<String, String>> doInBackground(String... jsonData) {

        List<HashMap<String, String>> places = null;

        PlaceJSONParser placeJsonParser = new PlaceJSONParser();

        try{
            jObject = new JSONObject(jsonData[0]);

            // Getting the parsed data as a List construct
            places = placeJsonParser.parse(jObject);

        }catch(Exception e){
            Log.d("Exception",e.toString());
        }
        return places;
    }

    @Override
    protected void onPostExecute(List<HashMap<String, String>> result) {

        String[] from = new String[] { "description"};
        int[] to = new int[] { android.R.id.text1 };

        // Creating a SimpleAdapter for the AutoCompleteTextView
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);

        // Setting the adapter
        atvPlaces.setAdapter(adapter);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}
4

5 回答 5

5

我刚刚尝试了一些搜索结果:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=123+43+Sweden&types=%28regions%29&sensor=false&key=AIzaSyDcXFSC2I6ZqxQeAbUMFvJKMrA98217H9U

URL 编码可能未正确编码空间。如果您在 get 中有类似“123+”的内容,它应该会产生如上所示的有效结果。相反,您需要进行替换以替换 + 的空格。

input=input.replace(" ","+");

在你的 URLEncoder 之后尝试保持编码,但消除 + 被编码的任何风险。

高温高压

于 2013-11-19T11:43:35.097 回答
4

尝试添加

adapter.notifyDataSetChanged();

atvPlaces.setAdapter(adapter);
于 2015-07-28T05:22:13.737 回答
1

尝试 ”。” 而不是空间。你应该用 dot(.) 替换空格,如果它不起作用你应该像在 ubuntu 中一样尝试“\”。可能有用

于 2013-11-20T12:44:20.137 回答
1

尝试替换此行

        input = "input=" + URLEncoder.encode(place[0], "utf-8");

经过

        input = "input=" + URLEncoder.encode(place[0]);
于 2013-11-22T12:53:42.117 回答
0

我喜欢一个解决方案,TextWatcher() 没有任何问题。

需要使用下面的代码在下面的代码中,我没有覆盖“performFiltering”方法,我添加了它,我的自动完成文本框现在工作正常。

公共类 CustomAutoCompleteView 扩展 AutoCompleteTextView {

public CustomAutoCompleteView(Context context) {  
    super(context);  
    // TODO Auto-generated constructor stub  
}  

public CustomAutoCompleteView(Context context, AttributeSet attrs) {  
    super(context, attrs);  
    // TODO Auto-generated constructor stub  
}  

public CustomAutoCompleteView(Context context, AttributeSet attrs,  
        int defStyle) {  
    super(context, attrs, defStyle);  
    // TODO Auto-generated constructor stub  
}  

@Override  
protected void performFiltering(final CharSequence text, final int keyCode) {  
    String filterText = "";  
    super.performFiltering(filterText, keyCode);  
}  
/** 
* After a selection, capture the new value and append to the existing 
* text 
*/  
@Override  
protected void replaceText(final CharSequence text) {  
    super.replaceText(text);  
}  

}

于 2015-10-07T07:31:42.667 回答