0

I have asked question two days ago at here

I didnt get any solution. Now I have tried one another example. MainActivity.java

public class MainActivity extends Activity {
String url;
private static final String TAG_RESULT = "predictions";
JSONObject json;
JSONArray contacts = null;
AutoCompleteTextView ed;
String[] search_text;
ArrayList<String> names;
ArrayAdapter<String> adp;
String browserKey="AIzaSyBdOTK4q66CloIbg1y71YSsy03yodXKxiI";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ed=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    ed.setThreshold(0);
    names=new ArrayList<String>();
        ed.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= ed.getText().toString().split(",");
           url="https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+search_text[0]+"&location=37.76999,-122.44696&radius=500&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;
              }
            };


        ed.setAdapter(adp); 


    }
    }


}

and

JSONParser.java

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
}

Running above example does not give me any suggestion.

here I am pasting my LogCat trace

03-01 17:34:07.546: E/Buffer Error(6043): Error converting result java.lang.NullPointerException: lock == null
03-01 17:34:07.546: E/JSON Parser(6043): Error parsing data org.json.JSONException: End of input at character 0 of 
03-01 17:34:07.835: D/URL(6043): https://maps.googleapis.com/maps/api/place/autocomplete/json?input=moda&location=37.76999,-122.44696&radius=500&sensor=true&key=AIzaSyBdOTK4q66CloIbg1y71YSsy03yodXKxiI
03-01 17:34:07.843: W/System.err(6043): java.net.UnknownHostException: Unable to resolve host "maps.googleapis.com": No address associated with hostname
03-01 17:34:07.843: W/System.err(6043):     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
03-01 17:34:07.851: W/System.err(6043):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-01 17:34:07.851: W/System.err(6043):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-01 17:34:07.851: W/System.err(6043):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-01 17:34:07.851: W/System.err(6043):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-01 17:34:07.851: W/System.err(6043):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-01 17:34:07.851: W/System.err(6043):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-01 17:34:07.851: W/System.err(6043):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-01 17:34:07.851: W/System.err(6043):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-01 17:34:07.851: W/System.err(6043):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-01 17:34:07.851: W/System.err(6043):     at com.example.autocompleted.JSONParser.getJSONFromUrl(JSONParser.java:38)
03-01 17:34:07.851: W/System.err(6043):     at com.example.autocompleted.MainActivity$paserdata.doInBackground(MainActivity.java:83)
03-01 17:34:07.851: W/System.err(6043):     at com.example.autocompleted.MainActivity$paserdata.doInBackground(MainActivity.java:1)
03-01 17:34:07.851: W/System.err(6043):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-01 17:34:07.851: W/System.err(6043):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-01 17:34:07.851: W/System.err(6043):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-01 17:34:07.851: W/System.err(6043):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-01 17:34:07.851: W/System.err(6043):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-01 17:34:07.851: W/System.err(6043):     at java.lang.Thread.run(Thread.java:856)
03-01 17:34:07.851: W/System.err(6043): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
03-01 17:34:07.859: W/System.err(6043):     at libcore.io.Posix.getaddrinfo(Native Method)
03-01 17:34:07.859: W/System.err(6043):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
03-01 17:34:07.859: W/System.err(6043):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
03-01 17:34:07.859: W/System.err(6043):     ... 18 more
03-01 17:34:07.859: W/System.err(6043): Caused by: libcore.io.ErrnoException: getaddrinfo failed: ENETUNREACH (Network is unreachable)
03-01 17:34:07.859: W/System.err(6043):     ... 21 more
03-01 17:34:07.859: E/Buffer Error(6043): Error converting result java.lang.NullPointerException: lock == null
03-01 17:34:07.859: E/JSON Parser(6043): Error parsing data org.json.JSONException: End of input at character 0 of 
03-01 17:34:23.257: W/IInputConnectionWrapper(6043): getTextBeforeCursor on inactive InputConnection
03-01 17:34:23.273: W/IInputConnectionWrapper(6043): getCursorCapsMode on inactive InputConnection
03-01 17:34:23.312: W/IInputConnectionWrapper(6043): getCursorCapsMode on     inactive InputConnection

I want immediate help as I am stucked at this point from last 4 days

Community
  • 1
  • 1

1 Answers1

0

Your code is perfectly fine.

Putting your API request:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=moda&location=37.76999,-122.44696&radius=500&sensor=true&key=AIzaSyBdOTK4q66CloIbg1y71YSsy03yodXKxiI

In REST client fetches out this result:

{
error_message: "The provided API key is expired."
predictions: [0]
status: "REQUEST_DENIED"
}

Please note that You'll need your own browser or server API key for the Google Places API before you can begin using the Place Autcomplete service. Note that an Android API key will not work for the Place Autocomplete service.

Please visit this link to get your API key for browser.

For more information regarding this part visit this the bottom section of this page.

AniV
  • 3,997
  • 1
  • 12
  • 17