2013-03-14 82 views
2

我想傳遞地址並希望經緯度。但是我從maps.google.com獲得了完整的String。但是JSON格式存在錯誤。JSON格式錯誤

我試試下面的代碼

Error :Couldn't get connection factory client 

getLocationInfo( 「3太陽城Nollez法國巴黎」);

public static void getLocationInfo(String address) { 
     StringBuilder stringBuilder = new StringBuilder(); 
     try { 

     address = address.replaceAll(" ","%20");  

     HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); 
     HttpClient client = new DefaultHttpClient(); 
     HttpResponse response; 
     stringBuilder = new StringBuilder(); 


      response = client.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      InputStream stream = entity.getContent(); 
      int b; 
      while ((b = stream.read()) != -1) { 
       stringBuilder.append((char) b); 
      } 

      System.out.print(stringBuilder); 
     } catch (ClientProtocolException e) { 
     } catch (IOException e) { 
     } 

     JSONObject jsonObject = new JSONObject(); 
     try { 

      jsonObject = new JSONObject(stringBuilder.toString()); 

      longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
        .getJSONObject("geometry").getJSONObject("location") 
        .getDouble("lng"); 

       System.out.print(longitute); 


       latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
        .getJSONObject("geometry").getJSONObject("location") 
        .getDouble("lat"); 

       System.out.print(latitude); 

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

     // return jsonObject; 
    } 

logcat的

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "3", 
       "short_name" : "3", 
       "types" : [ "street_number" ] 
      }, 
      { 
       "long_name" : "Cité Nollez", 
       "short_name" : "Cité Nollez", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "18th arrondissement of Paris", 
       "short_name" : "18th arrondissement of Paris", 
       "types" : [ "sublocality", "political" ] 
      }, 
      { 
       "long_name" : "Paris", 
       "short_name" : "Paris", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Paris", 
       "short_name" : "75", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "Ã?le-de-France", 
       "short_name" : "IdF", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "France", 
       "short_name" : "FR", 
       "types" : [ "country", "political" ] 
      }, 
      { 
      "long_name" : "75018", 
       "short_name" : "75018", 
       "types" : [ "postal_code" ] 
      } 
     ], 
     "formatted_address" : "3 Cité Nollez, 75018 Paris, France", 
     "geometry" : { 
      "location" : { 
       "lat" : 48.89376110, 
       "lng" : 2.33742180 
      }, 
      "location_type" : "ROOFTOP", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 48.89511008029150, 
        "lng" : 2.338770780291502 
       }, 
       "southwest" : { 
        "lat" : 48.89241211970850, 
        "lng" : 2.336072819708498 
       } 
      } 
     }, 
     "types" : [ "street_address" ] 
     } 
    ], 
    "status" : "OK" 
} 
03-14 13:51:35.776: I/MapActivity(1430): Handling network change notification:CONNECTED 
03-14 13:51:35.776: E/MapActivity(1430): Couldn't get connection factory client 
+0

請添加一個logcat請 – Nezam 2013-03-14 08:24:23

+0

您是否添加了互聯網權限?' – Naveen 2013-03-14 08:25:45

+0

您有沒有提到過「但JSON格式有錯誤「你能附上收到的JSON字符串嗎? – vinaykumar 2013-03-14 08:29:04

回答

2

在你的地圖活動添加以下代碼super.onCreate()

爲ExampleActivity之前: -

public class ExampleActivity extends Activity { 

    // url to make request 
    private static String url = "http://maps.googleapis.com/maps/api/geocode/json?address="+address.replace(" ", "%20")+"&sensor=true"; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     tvlat = (TextView) findViewById(R.id.tvlat); 
     tvlng = (TextView) findViewById(R.id.tvlng); 

     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 

     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 

     try { 
      System.out.println("Location Latitude" 
       + json.getJSONArray("results").getJSONObject(0) 
         .getJSONObject("geometry") 
         .getJSONObject("location").getString("lat")); 

       System.out.println("Location Longitude" 
       + json.getJSONArray("results").getJSONObject(0) 
         .getJSONObject("geometry") 
         .getJSONObject("location").getString("lng"));  
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

JSONPareser: -

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; 

    } 
} 

然後跑你的應用程序,它爲我工作得很好。

+0

Right ans。但我想在我的問題解決方案 – 2013-03-14 11:06:54

+0

如果您的應用程序運行完美也會得到這兩條消息第一個是信息,下一個是錯誤消息。你可以說你正在面臨的問題是什麼? – Harish 2013-03-14 11:53:06

+0

+1但未接受此答案,因爲我想在我的問題中回答 – 2013-03-14 12:24:26

0

是否啓用這些權限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
0

在使用地圖時,如果「無法獲取連接錯誤」,則無需擔心。即使發生該事件,它也能正常工作。如果您的代碼不起作用,那麼還有其他原因。檢查它..