Adapt code to google maps json url

Asked

Viewed 75 times

1

I followed the code that was in the first comment Link to the comment

worked well but when I tried instead of passing the json from wikipedia pass the json from google maps no longer appear results I think the error is in the input

 input = "input=" + constraint;

I leave my code here :

public class oi extends Activity {
    TextView teste;
    AutoCompleteTextView atvPlaces;


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oi);

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


        AutoCompleteTextView actv = new AutoCompleteTextView(this);
        actv.setThreshold(1);
        String[] from = {"name", "description"};
        int[] to = {android.R.id.text1, android.R.id.text2};
        SimpleCursorAdapter a = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, null, from, to, 0);
        a.setStringConversionColumn(1);
        FilterQueryProvider provider = new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence constraint) {
                // run in the zbackground thread
                if (constraint == null) {
                    return null;
                }


                String[] columnNames = {BaseColumns._ID, "name", "description"};
                MatrixCursor c = new MatrixCursor(columnNames);
                try {
                    String data = "";

                    String key = "key=AIzaSyAQ_ZcIMsbGELnmPkk-43UwKVXlJbu1wwA";

                    String input="";

                    input = "input=" + constraint;




                    // place type to be searched
                    String types = "types=geocode";

                    // Sensor enabled
                    String sensor = "sensor=false";
                    input=input.replace(" ","+");
                    // 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 urlString = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;


                    URL url = new URL(urlString);
                    InputStream stream = url.openStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                    String jsonStr = reader.readLine();
                    // output ["query", ["n0", "n1", ..], ["d0", "d1", ..]]
                    JSONArray json = new JSONArray(jsonStr);
                    JSONArray names = json.getJSONArray(1);
                    JSONArray descriptions = json.getJSONArray(2);
                    for (int i = 0; i < names.length(); i++) {
                        c.newRow().add(i).add(names.getString(i)).add(descriptions.getString(i));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return c;
            }
        };
        a.setFilterQueryProvider(provider);
        actv.setAdapter(a);
        setContentView(actv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    }
}
  • solved your doubt?

  • Solved your doubt?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.