2014-05-14 55 views
0

我們試圖使用Google Places API來查找附近的地方,並且在運行時只是作爲一個Java項目工作。但是,在android應用程序中的按鈕的onClick()函數下運行同一段代碼會導致我們從Google返回的JSON字符串爲空。硬編碼GPS座標用於兩者。API調用在java項目中工作,但在Android應用程序內調用時不會調用

這是爲什麼?

這是試圖抓住JSON數據

05-14 00:54:12.828: E/PlacesService(8677): org.json.JSONException: End of input at character 0 of 
05-14 00:54:12.828: E/PlacesService(8677): at org.json.JSONTokener.syntaxError(JSONTokener.java:450) 
05-14 00:54:12.828: E/PlacesService(8677): at org.json.JSONTokener.nextValue(JSONTokener.java:97) 

這就是API調用時顯示的錯誤:

private String makeUrl(double latitude, double longitude, String place) { 
    StringBuilder urlString = new StringBuilder(
      "https://maps.googleapis.com/maps/api/place/search/json?"); 

    if (place.equals("")) { 
     urlString.append("&location="); 
      urlString.append(Double.toString(latitude)); 
      urlString.append(","); 
      urlString.append(Double.toString(longitude)); 
      urlString.append("&radius=1000"); 
      // urlString.append("&types="+place); 
      urlString.append("&sensor=false&key=" + API_KEY); 

    } else { 
     urlString.append("?types=" + "bar" + "|" + "night_club"); 
     urlString.append("&location="); 
     urlString.append(Double.toString(latitude)); 
     urlString.append(","); 
     urlString.append(Double.toString(longitude)); 
     urlString.append("&radius=1000"); 
     urlString.append("&sensor=false&key=" + API_KEY); 
    } 
    return urlString.toString(); 
} 

protected String getJSON(String url) { 
    return getUrlContents(url); 
} 

private String getUrlContents(String theUrl) { 
    StringBuilder content = new StringBuilder(); 

    try { 
     URL url = new URL(theUrl); 
     URLConnection urlConnection = url.openConnection(); 
     BufferedReader bufferedReader = new BufferedReader(
       new InputStreamReader(urlConnection.getInputStream()), 8); 
     String line; 
     while ((line = bufferedReader.readLine()) != null) { 
      content.append(line + "\n"); 
     } 
     bufferedReader.close(); 
    }catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return content.toString(); 
} 

下面是我們如何調用這些方法上面搶JSON字符串:

String urlString = makeUrl(32.8400, -117.2769 placeSpacification); 
    String json = getJSON(urlString); 

這是我們的AndroidManifest.xml。我們已包含必要的位置和Internet權限。

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="19" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name="com.example.testapp.ListViewAndroidExample" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

謝謝!

+1

我懷疑這是問題,但建議(在以後的版本中需要)在後臺線程上執行網絡操作,比如使用AsyncTask。即使它不能解決這個問題,反正也是件好事。 – nasch

+0

這是如何工作的?你知道任何使用Asynctask的教程嗎? – user1530318

回答

0

嘿添加Aquery jar到您的項目,並嘗試下面的代碼。

latitude =18 ; 
longitude = 50 ; 
StringBuilder builder = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); 
    builder.append("location="+latitude+","+longitude); 
    builder.append("&radius=100"); 
    builder.append("&sensor=true"); 
    builder.append("&types="+type); 
    builder.append("&key=XXXXXXXXXXXXXXXXXXXXX"); 

    String url = builder.toString(); 

aQuery.progress(progressDialog).ajax(url, JSONObject.class, new AjaxCallback<JSONObject>(){ 

     @Override 
     public void callback(String url, JSONObject object, 
       AjaxStatus status) { 
      // TODO Auto-generated method stub 
      super.callback(url, object, status); 

      //System.out.println("objectttt ==== "+object); 

      JSONArray jsonArray; 

      try { 

       jsonArray = object.getJSONArray("results"); 
       for(int j=0; j<jsonArray.length(); j++) 
       { 
        JSONObject jsonObject = jsonArray.getJSONObject(j); 
        String placeName = jsonObject.getString("name"); 
        String placeAddress= jsonObject.getString("vicinity"); 
        String image=jsonObject.getString("icon"); 

        JSONObject jsonObject2 = jsonObject.getJSONObject("geometry"); 
        JSONObject jsonObject3 =jsonObject2.getJSONObject("location"); 

        for(int i=0 ;i<jsonObject3.length();i++) 
        { 
         PlaceLatitude =jsonObject3.getDouble("lat"); 
         PlaceLongitude =jsonObject3.getDouble("lng"); 

        } 

        try 
        { 
         imgURL= new URL(image); 

        } catch (MalformedURLException e) 
        { 
         e.printStackTrace(); 
        } 

        arrPlaces.add(placeName); 
        arrPlaceImage.add(imgURL); 
        arrPlaceAddress.add(placeAddress); 
        arrPlaceLatitude.add(PlaceLatitude); 
        arrPlaceLogitude.add(PlaceLongitude); 


       } 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    }); 
相關問題