2012-04-25 155 views
-1

晚上好,如何實現谷歌Places API的

我一直在努力實現利用谷歌的地方(TM)API來確定的利益(如餐館和酒店等地方點簡單的Android(TM)應用程序),但是我在確定如何開始實施方面遇到了很多麻煩。是我已經使用

資源如下:

- The Google Places(tm) documentation

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

-The的Android異步任務(TM)開發者文檔從

- 各種其他計算器帖子個人處於類似情況下

我簡直是一跳並提供一些指導,這樣任何人在我的情況下都可以簡單地找到這篇文章,然後轉發給一些非常有見地的資源。

此外,我覺得使用Google搜索找到資源的效率有點低,有沒有其他數據庫是程序員經常使用的,我不知道?任何推薦的文獻?

TL; DR ...

- 我在尋找一個使用Places API,用JSON對象的工作和Android(TM)平臺上聯網的一些明確的指導。

- 我希望能被重定向到其他人以前發現的一些有用的資源。

-I已包括我的Main.java文件下面簡單地提供背景 - 我不是在尋找答案:)


代碼實現了Places API的功能Main.java類:

 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); 

     } 
    }); 

代碼的結果類處理異步任務:

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; 
    } 
}  

當我點擊模擬器中的「食物」按鈕(如上所述)時,目前收到一個android.os.NetworkOnMainThreatException。

任何建議都非常受歡迎,我試圖在Android平臺上打造一個非常穩固的基礎。

+0

你在哪裏做httppost從服務器gtting JSON? – 2012-04-25 02:52:12

+0

僅當您嘗試在主線程中執行網絡任務時發生NetworkOnMainThreatException:http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – anticafe 2012-04-25 03:24:45

回答

1

您沒有正確使用AsyncTask。 Android的文檔明確表示do't調用AsyncTask方法manually.you呼籲doInBackground通過創建AsyncTask對象改變你的代碼是這樣的:

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; 
     } 
} 
+0

感謝您的及時響應! 我不知道如何爲json對象創建一個httppost帖子。我正在尋找一種資源來幫助我確定如何使用通過API呈現的數據,因此我的帖子在這裏。 有沒有解釋如何在Android中實現這些請求的具體來源? – Squagem 2012-04-25 03:30:49

+0

看到這個例子http://www.instropy.com/2010/06/14/reading-a-json-login-response-with-android-sdk/並使用runOnUiThread或AsyncTask來製作httppost – 2012-04-25 03:41:21

+0

請參閱我的編輯答案 – 2012-04-25 03:59:33