2014-02-27 1 views
0

안녕하세요 저는 역방향 Google 지오 코더를 사용하며 도시 이름이 예를 들어 'Königsaich'인 경우 지오 코더는 'Königsaich'라고 이름을 지정합니다. "¶"문자를 필터링하고 올바른 문자 "ö"로 바꾸려면 어떻게해야합니까?문자열에서 "¶"과 같은 특수 문자를 대체하고 올바른 문자 "Ö"를 부여하는 방법은 무엇입니까?

편집 :

내 코드 :

public JSONObject getJSONCity(double lat, double lon){ 

     HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lon+"&sensor=false"); 
      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) { 
      } 

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


    public String getCityname(double lat, double lon){ 

     JSONObject ret = getJSONCity(lat,lon); 
     JSONArray jsonArray; 

     JSONObject location; 
     String location_string; 

     try { 
      jsonArray = ret.getJSONArray("results").getJSONObject(0).getJSONArray("address_components"); 
      cityname = jsonArray.getJSONObject(2).getString("long_name"); 

      //Get JSON Array called "results" and then get the 0th complete object as JSON   
      location = ret.getJSONArray("results").getJSONObject(0); 
      // Get the value of the attribute whose name is "formatted_string" 
      location_string = location.getString("formatted_address"); 

      System.out.println(" results: " + location_string); 

      System.out.println(" My City: " + cityname); 
     } catch (JSONException e1) { 
      e1.printStackTrace(); 
     } 
     return cityname; 
    } 

사람이 나에게 예를 들어 주실 수 있습니까?

+4

에있다 - 일반적으로는 "바이트를 가지고 어디에 텍스트 변환 ". Geocoder API를 사용하는 방법을 알려주세요. –

+0

러시아 이름이 잘못 나오는 동안 오류가 발생했다고 생각하십니까? –

+0

나는 정규 표현식 개념을 적용 할 수 있다고 생각합니다. –

답변

0
String actualstring ="what ever your string which you want to replace here get "; 
     if(actualstring.contains("here your string which u want to chk")); 
       { 
        // it chk weather it contain that string which u want to replace 
      actualstring.replace("target", "replacement"); 
       } 
+0

이 문제는'HttpEntity'에서 바이트를 읽고 문자로 처리 할 때 수천 가지의 문자가 잘못 읽혀질 수 있다는 것이다. 이 코드 조각을 수천, 수천 시간을 포함하여 제안 하시겠습니까? 이 문제를 해결하는 것은 완전히 잘못된 방법입니다. –

0

는 시도이

str = str.replace(' ','_'); 

공공 문자열 (CharSequence를 대상, CharSequence를 교체) API 레벨 추가 1 복사 지정된 대상 시퀀스의 발생을 대체 문자열을 대체 다른 순서로. 문자열은 처음부터 끝까지 처리됩니다.

매개 변수 바꾸려는 시퀀스를 대상으로 지정하십시오. 교체 시퀀스를 대체하십시오.

결과 문자열 을 반환합니다. 예외가 발생했을 경우 NullPointerException - target 또는 replacement가 null 인 경우.

0

귀하의 문제는 귀하의 HttpEntity에서 바이트를 읽고 각 바이트가 단일 문자라고 가정합니다. 인코딩에 따라 ASCII 문자는 작동하지만 다른 많은 문자는 실패 할 수 있습니다.

이 작업을 올바르게 수행하려면 정교한 검색/바꾸기 조합을 설정하는 대신 Apache의 HTTP Utils 라이브러리와 같은 라이브러리를 사용하고 싶습니다. 여기에는 HttpEntity에서 문자열을 읽는 몇 가지 방법이있는 클래스 EntityUtils이 포함되어 있습니다.

당신이 문자열에 잘못된 데이터를 가지고 있다면 그것을위한 Javadoc, 당신은 다시 걸음을 이전하는 과정에서이를 수정해야 http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html

관련 문제