2014-10-06 38 views
0

在這我嘗試擊中URL bacground「response1」,但response1是打印空值,最新的問題。但是「HttpRes」也是打印在url中添加地址。請回復最新的問題。谷歌文本搜索URL在android中不工作

在我的代碼JSON不顯示在logcat時使用文本搜索網址我在上面的代碼中有什麼問題............................ ..................

10-06 09:50:56.391:I/System.out(19964):GeocoderTask latlnglat/lng:(19.0759837,72.8776559) 10-06 09:50:56.395:I/System.out(19964):AddressText === >>>孟買,馬哈拉施特拉邦,印度10-06 09:50:56.399:I/System.out(19964):HttpRes response ---- >>>>https://maps.googleapis.com/maps/api/place/textsearch/json?query=Mumbai,馬哈拉施特拉邦,印度& key = AIzaSyAoXVbnsD_AV8ejPliFjT3vIEtEXsv1lPc 10-06 09:50:56.399:I/System.out(19964):Addresstext response ---- >>>> null 10-06 09 :50:56.399:I/System.out(19964):Geocoder doInBackground response null

private class GeocoderTask extends AsyncTask<String, Void, String>{ 

     String location,response1= null; 

     String addressText; 
    /**********************************************************/ 
    public GeocoderTask(String location) { 
     this.location=location; 
    } 

    @Override 
    protected void onPreExecute() { 

     myServiceToHttp= new MyServiceToHttp(); 
     List<Address> addresses = null; 
     Geocoder geocoder = new Geocoder(getBaseContext());  
    /**********************************************************/ 
     try { 
      addresses = geocoder.getFromLocationName(location,1000); 


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

     if(addresses==null || addresses.size()==0){ 
      Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show(); 
      googleMap.clear(); 
     } 
     else { 
      if(addresses.size()>0) 
       for(int i=0;i<addresses.size();i++){ 
        Address address = (Address) addresses.get(i); 
        // Creating an instance of GeoPoint, to display in Google Map 
        latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
        lat = address.getLatitude(); 
        lng = address.getLongitude(); 
        addressText = String.format("%s, %s",address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",address.getCountryName()); 

        System.out.println(" GeocoderTask latlng" +latLng); 

        Toast.makeText(getApplicationContext(), "latlng" +String.valueOf(latLng), Toast.LENGTH_SHORT).show(); 
        Toast.makeText(getApplicationContext(), "Geolat == "+lat+ "Geolng == " +lng, Toast.LENGTH_SHORT).show(); 

        System.out.println("AddressText===>>>" +addressText); 

        Toast.makeText(getApplicationContext(), "addressText"+addressText, Toast.LENGTH_SHORT).show(); 

        marker = new MarkerOptions(); 
        marker.position(latLng); 
        marker.title(addressText); 

        googleMap.addMarker(marker); 
        // Locate the first location 
        if(i==0){ 

         CameraPosition cameraPosition = new CameraPosition.Builder() 
         .target(latLng)    // Sets the center of the map to Mountain View 
         .zoom(10)     // Sets the zoom 
         .bearing(40)    // Sets the orientation of the camera to east 
         .tilt(5)     // Sets the tilt of the camera to 30 degrees 
         .build();     // Creates a CameraPosition from the builder 
         googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

        } 


       } 

     } 


     super.onPreExecute(); 
    } 


    @Override 
    protected String doInBackground(String... locationName) { 


      response1 = myServiceToHttp.makeServiceCall("https://maps.googleapis.com/maps/api/place/textsearch/json?query="+addressText+"&key=YOUR SERVER KEY",MyServiceToHttp.GET); 

      String HttpRes = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="+addressText+"&key=YOUR SERVER KEY"; 
      System.out.println("HttpRes response---->>>>" +HttpRes); 
      System.out.println("Addresstext response---->>>>" +response1); 

      if (response1!=null) { 

       try { 

         JSONObject jsonObject=new JSONObject(response1); 
         System.out.println("Json==>>>>"+jsonObject); 
         JSONArray array=jsonObject.getJSONArray("results"); 
         System.out.println("JsonArray==>>>"+array); 

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

      } 
      else 
      { 
       System.out.println("Geocoder doInBackground response null"); 
      } 



     return response1; 
    } 

    @Override 
    protected void onPostExecute(String addresses) { 

     Toast.makeText(getApplicationContext(), addresses, Toast.LENGTH_SHORT).show(); 

     super.onPostExecute(addresses); 


    } 
} 

回答

0

你的API密鑰在哪裏?

試試這個

C:\Program Files\Java\jdk1.7.0_04\bin>keytool.exe -V -list -alias androiddebugkey -keystore "C:\Documents and Settings\IBM\.android\debug.keystore" -storepass android -keypass android 

考慮您的jdk-bin pathfirst path地方,在你的second pathhome user directory。對於windows 7它就像C:\Users\username\.android\debug.keystore

使用上述命令您將獲得所有的密鑰。

The google API key is here

然後

據我所看到的,移動地圖到某一點是通過調用GoogleMap對象的moveCamera()方法完成的:從this例如兩者

GoogleMap map = ((MapFragment) getFragmentManager() 
      .findFragmentById(R.id.map)).getMap(); 

LatLng sydney = new LatLng(-33.867, 151.206); 

map.setMyLocationEnabled(true); 
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); 

。你可以看到,你需要經緯度座標來創建一個對象LatLng。我相信你可以使用谷歌地點搜索來獲得這些座標,使用http-requests。例如:

https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere 

here

採取哪些應該返回JSON數據爲您服務。

對於自動完成,看this鏈接

編輯:佬,地點搜索將返回的XML,可以嘗試使用JSON ...查詢= ...而不是

+0

10-06 09:50:56.391:I/System.out(19964):GeocoderTask latlnglat/lng:(19.0759837,72.8776559)10-06 09:50:56.395:I/System.out(19964):AddressText === >>>孟買,馬哈拉施特拉邦,印度10-06 09:50:56.399:I/System .out(19964):HttpRes response ---- >>>> https://maps.googleapis.com/maps/api/place/textsearch/json?query=Mumbai,Maharashtra,India&key = AIzaSyAoXVbnsD_AV8ejPliFjT3vIEtEXsv1lPc 10-06 09: 50:56.399:I/System.out(19964):Addresstext response ---- >>>> null 10-06 09:50:56.399:I/System.out(19964):Geocoder doInBackground response 空值 – 2014-10-06 10:05:13

0

在我的代碼的Json不顯示在logcat時使用textsearch url 在我上面的代碼中有什麼問題..................................... .........

10-06 09:50:56.391:I/System.out(19964):GeocoderTask latlnglat/lng:(19.0759837,72.8776559) 10-06 09:50:56.395 :我/系統em.out(19964):AddressText === >>>孟買,馬哈拉施特拉邦,印度 10-06 09:50:56.399:I/System。(19964):HttpRes響應---- >>>>https://maps.googleapis.com/maps/api/place/textsearch/json?query=Mumbai,馬哈拉施特拉邦,印度& key = AIzaSyAoXVbnsD_AV8ejPliFjT3vIEtEXsv1lPc 10-06 09:50:56.399:I/System.out(19964):Addresstext response ----> >>> null 10-06 09:50:56.399:I/System.out(19964):Geocoder doInBackground response null