2012-01-06 22 views
0

我从Android HTTP获取一个长字符串。该字符串中有许多中文字,所以我在我的字符串中获得大量的密码。这使我的解析器不可行。我听说服务器的数据是一种原始数据。如何将翻译密码翻译成可读的代码(中文)?之后有一个Android HTTP获取....错误字符串

我试过以下,但它不起作用。

String retSrc = EntityUtils.toString(response.getEntity()); 
byte[] queryBytes = retSrc.getBytes("Raw Data"); 
String Str = new String(queryBytes,"UTF-8"); 
+0

您需要使用正确的编码。 – SLaks 2012-01-06 02:25:45

+0

但我应该怎么做才能得到正确的结果.... 我是Android的新手....对不起我的愚蠢.... – bbbbbird1 2012-01-06 02:27:15

回答

0
byte[] queryBytes = retSrc.getBytes("Raw Data"); 

原始数据是一个charset编码器?

编码是一个丑陋的问题,一些建议:

当字符串从网站上获得,尝试“UTF-8”或“GBK”编码来。 如果打印到控制台的字符串是正确的,也许编码是正确的。

+0

我试过 'byte [] queryBytes = retSrc。 getBytes(“gbk”);' 'String Str = new String(queryBytes,“utf-8”);' and 'byte [] queryBytes = retSrc.getBytes(“gbk”);' 'String Str =新字符串(queryBytes,“UTF-8”);' 中文返回字符串仍然保持加扰 但是....它是不同的争夺代码.....但仍然炒...冏 – bbbbbird1 2012-01-06 02:42:24

+0

你在最初读取的时候,加上编码,得到字符串以后,不用再转码 – idiottiger 2012-01-06 02:45:48

+0

可是,加上编码以后,他变成一个Byte的阵列,不是String .....怎么把它变成String ... ....? – bbbbbird1 2012-01-06 02:47:33

0

您需要知道页面内容的正确编码,例如。 Big5,GB2312或UTF-8,然后尝试以下功能获取页面内容:

public String getURLContent(String URL, String encoding) { 
     StringBuilder builder = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(URL); 
     try { 
      HttpResponse response = client.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(
           response.getEntity().getContent(), encoding)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        builder.append(line + "\r\n"); 
       } 
      } else { 
       System.out.println("Failed to download file"); 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    return builder.toString(); 
} 
+0

感谢您的帮助! – bbbbbird1 2012-01-06 04:05:15

相关问题