2015-04-06 27 views
0

我是android平臺的新手。我使用下面的代碼從谷歌地址,但我得到奇怪的字符。我很確定它將utf-8字符集設置爲代碼,但我不知道如何。我讀了類似的主題,提出:在http-get嘗試中設置utf-8字符集

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 

但下面的代碼就像魅力。任何想法如何把charcet utf-8;對不起,這個基本的問題,並提前感謝。

HttpGet httpGet = new HttpGet(
         "http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+ "&sensor=true"); 
       HttpClient client = new DefaultHttpClient(); 
       HttpResponse response; 
       StringBuilder stringBuilder = new StringBuilder(); 

       try { 
        response = client.execute(httpGet); 
        HttpEntity entity = response.getEntity(); 
        InputStream stream = entity.getContent(); 
        int b; 
        while ((b = stream.read()) != -1) { 
         stringBuilder.append((char) b); 
        } 
       } catch (ClientProtocolException e) { 
       } catch (IOException e) { 
       } 


       try { 
        jsonObject = new JSONObject(stringBuilder.toString()); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 



      try { 
       location = jsonObject.getJSONArray("results").getJSONObject(0); 
       location_string = location.getString("formatted_address"); 
       Log.d("test", "formattted address:" + location_string); 
      } catch (JSONException e1) { 
       e1.printStackTrace(); 

      } 
+0

你能解釋你的問題是什麼,你需要什麼? –

回答

0

你在正確的軌道上。這將是這樣的:

  try { 
      response = client.execute(httpGet); 
      HttpEntity entity = response.getEntity(); 
      InputStream stream = entity.getContent(); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(stream,"UTF-8")); 

      /* 
      int b; 
      while ((b = stream.read()) != -1) { 
       stringBuilder.append((char) b); 
      } 
      */ 

      String line=null; 

      while ((line = reader.readLine()) != null) { 
       stringBuilder.append(line); 
      } 

      stream.close(); 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 
+0

非常感謝Daniel – johnnal

+0

@ johnnal很高興能幫到你! –