-1

Good Evening,

I have been trying to implement a simple Android(tm) application that utilizes Google Places(tm) API to determine local points of interest (such as restaurants and hotels), however I have been having quite a lot of trouble determining how to actually get started.

Resources I have already employed are as follows:

-The Google Places(tm) documentation

-An Android(tm) development blog hosted by Brain Buikema

-The Android(tm) developer documentation on asynchronous tasks

-Various other stackoverflow postings from individuals in similar circumstances

I was simply hoping for some guidance so that anyone in my situation could simply find this post and maybe then be forwarded to some extremely insightful resources.

Further, I feel that using Google searches to find resources is somewhat inefficient, are there other databases out there that programmers frequently utilize of which I am not aware? Any recommended literature?

TL;DR...

-I'm looking for some definitive guides for using the Places API, working with JSON objects and networking on the Android(tm) platform.

-I was hoping to be redirected to some useful sources others have found in the past.

-I have included my Main.java file below simply to provide context - I am not looking for answers :)


Code for the Main.java class that implements the Places API functionality:

        Button food = (Button) findViewById(R.id.button14);

        food.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            type = "restaurant";

            //Constructs the urlString
            if(useCurr)
                urlString = stringConstructor(myKey, lat, lon, radius, sensor, type);
            else
            {
                //Here, you must update the lat and lon values according to the location input by the user
                urlString = stringConstructor(myKey, lat, lon, radius, sensor, type);
            }



            //DO HTTPS REQUEST HERE
            urlString = "https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAp24M3GU9V3kWrrezye8CyUrhtpToUhrs";

            Results res = new Results();

            JSONArray json = res.doInBackground(urlString);

            System.out.println(json);

        }
    });

Code for the Result class that handles the Asynchronous task:

private class Results extends AsyncTask<String, Integer, JSONArray>
{
    protected JSONArray doInBackground(String...params)
    {
        JSONArray output = null;

        try
        {
            try
            {
                url = new URL(params[0]);   
            }
            catch(MalformedURLException e)
            {
                System.out.println("URL formed incorrectly! (" + e + ")");
            }

            output = (JSONArray) url.getContent();
        }
        catch(Exception e)
        {
            System.out.println("Exception: " + e);
        }

        return output;
    }
}    

Currently receiveing an android.os.NetworkOnMainThreatException whenever I click on the "Food" button (described above) in the emulator.

Any advice is greatly welcomed, I am trying to build a really solid foundation on the Android platform.

Squagem
  • 724
  • 4
  • 17
  • where you are making httppost for gtting json from server? – ρяσѕρєя K Apr 25 '12 at 02:52
  • FYI: NetworkOnMainThreatException occurs when you try to execute network task in main thread: http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – anticafe Apr 25 '12 at 03:24

1 Answers1

1

You are not using AsyncTask in right way. android doc clearly says do't call AsyncTask methods manually.you are calling doInBackground by creating object of AsyncTask change your code this way:

 btnclicl.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    Results  dTask = new Results();  
                    dTask.execute(urlString);  
                }  
            });  

    class Results  extends AsyncTask<Integer, Integer, String>{  

            @Override  
        protected void onPostExecute(JSONObject json) {  
            Log.v("json", json);
            super.onPostExecute(json); 
        }  
        @Override  
        protected JSONObject doInBackground(String... params) { 
            JSONObject strtemp=null;            
          HttpClient httpclient = new DefaultHttpClient();
    // Prepare a request object
    HttpGet httpget = new HttpGet(urlString); 
    // Execute the request
    HttpResponse response;
    JSONObject json = new JSONObject();
    try {
        response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();

        if (entity != null) {

            // A Simple JSON Response Read
            InputStream instream = entity.getContent();
            String result= convertStreamToString(instream);

            json=new JSONObject(result);

            instream.close();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return json;
        }  
}  
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks for the prompt response! I am unaware of how to make an httppost post for a json object. I was looking for a resource to aid me in determining how to work with the data as presented through the API, hence my post here. Is there any specific source that explains how to implement these requests in Android? – Squagem Apr 25 '12 at 03:30
  • see this example http://www.instropy.com/2010/06/14/reading-a-json-login-response-with-android-sdk/ and use runOnUiThread or AsyncTask for makeing httppost – ρяσѕρєя K Apr 25 '12 at 03:41
  • Thank you very much! Although I was not looking for a specific answer to the code, the resources with which you have provided reaffirm much of which I was unsure. – Squagem Apr 25 '12 at 20:58