Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在有一个Android HTTP Get…字符串错误之后_Android - Fatal编程技术网

在有一个Android HTTP Get…字符串错误之后

在有一个Android HTTP Get…字符串错误之后,android,Android,我从Android HTTP Get获取一个长字符串。字符串中有很多中文单词,所以我在字符串中得到了很多加扰码。这使得我的解析器无法工作。我听说服务器上的数据是一种原始数据。如何将加扰代码翻译成可读代码(中文) 我试过下面这个,但不起作用 String retSrc = EntityUtils.toString(response.getEntity()); byte[] queryBytes = retSrc.getBytes("Raw Data"); String Str = new S

我从Android HTTP Get获取一个长字符串。字符串中有很多中文单词,所以我在字符串中得到了很多加扰码。这使得我的解析器无法工作。我听说服务器上的数据是一种原始数据。如何将加扰代码翻译成可读代码(中文)

我试过下面这个,但不起作用

 String retSrc = EntityUtils.toString(response.getEntity());
 byte[] queryBytes = retSrc.getBytes("Raw Data");
 String Str = new String(queryBytes,"UTF-8");
原始数据是字符集编码器吗

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

从站点获取字符串时,请尝试使用“utf-8”或“gbk”进行编码。
如果打印到控制台的字符串正确,则编码可能正确。

您需要知道页面内容的正确编码,例如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();
}

你需要使用正确的编码。但是我应该如何做才能得到正确的结果。。。。我是一个Android新手…很抱歉我的愚蠢…我试过使用
byte[]queryBytes=retSrc.getBytes(“gbk”)
String Str=新字符串(查询字节,“utf-8”)
字节[]queryBytes=retSrc.getBytes(“gbk”)
String Str=新字符串(查询字节,“utf-8”)返回字符串在中文中仍然是加扰的,但是…它是不同的加扰码…但是仍然是加扰的。。。冏你在最初讀取的時候,加上編碼,得到一串以後,不用再轉碼是這樣的,就是你從網路上最開始的時候是流,你在從 流->一串時候這個時候加入編碼,那就直接就是編碼好的 一串了<代码>字符串retSrc=EntityUtils.toString(response.getEntity())網路上說這邊就已經算用utf-8編碼了@@。。是我看錯了嗎。。求大大指導如果默認是 UTF-8那你得到一串以後如果是正確的,那編碼就是對的,如果還是有問題的話,你改成 字符串retSrc=EntityUtils.toString(response.getEntity(),“gbk”)試試
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();
}