json Parsing中的特殊人物

时间:2011-05-09 15:11:14

标签: android

如何解析json解析中的特殊字符

内容在utf-8中被编码。虽然解码被炒掉而且显得很糟糕但我正在变得干脆? 我的代码如下:

inputStream = (InputStream) new URL(url).getContent();
              return  convertStreamToString(inputStream);

  //converting into string
public static String convertStreamToString(InputStream is) throws IOException {
        String serverResponse=null;
        try{
//String serverResponse;
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            serverResponse= writer.toString();

            Log.e("response from server", serverResponse);
        } else {
            serverResponse= "{\"Error\":\"No response\"}";
        }
        return serverResponse;
        }catch(IOException e){
//          timeOut=true;
            serverResponse= "{\"Error\":\""+e.toString()+"\"}";
            Log.e("Red spcie utils converting to string failed",e.toString()    );
        }
        return serverResponse;
    }

我到了吗?例如,在内容中标记 - man?o(芒果)

1 个答案:

答案 0 :(得分:0)

谢谢你Jon Skeet你是对的。我通过使用解码器ISO8859_1获得了解决方案 在

public static String convertStreamToString(InputStream is)throws IOException {         String serverResponse = null;         尝试{ // String serverResponse;         if(is!= null){             Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is,"ISO8859_1"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        serverResponse= writer.toString();

        Log.e("response from server", serverResponse);
    } else {
        serverResponse= "{\"Error\":\"No response\"}";
    }
    return serverResponse;
    }catch(IOException e){

// timeOut = true;             serverResponse =“{\”Error \“:\”“+ e.toString()+”\“}”;             Log.e(“Red spcie utils转换为字符串失败”,e.toString());         }         return serverResponse;     }