Java 里StringBuffer.toString总是乱码

各位好,这是我在网上看到的一个输入框提示的程序,我改了一下,content总是乱码,哪里的编码出问题了呢?我试着把content装成bytes[]在转成string还是乱码。

package com.bobliao;

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import org.apache.http.client.ClientProtocolException;


import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
public class SuggestionTest extends JFrame {
    
    private JTextField textInput=new JTextField();
    private JList<String> listSuggestions=new JList<>(); 
    
    public SuggestionTest() {
        
        super("SuggestionTest");
        getContentPane().add(BorderLayout.NORTH,textInput);
        getContentPane().add(BorderLayout.CENTER,
                new JScrollPane(listSuggestions));
        setSize(300,200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        
        this.textInput.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e){
                new Thread(() -> {
                    try{
                        String word=textInput.getText();
                        //word打印出来
                        System.out.println(word);
                        String[] suggestions=fetchSuggestionsFromBaidu(word);
                        SwingUtilities.invokeLater(() -> {
                            listSuggestions.removeAll();
                            listSuggestions.setListData(suggestions);
                            listSuggestions.updateUI();
                        });
                    }
                    catch (Exception ex) {
                        // TODO: handle exception
                        ex.printStackTrace();
                        
                    }
                }).start();
            }
            
            public String[] fetchSuggestionsFromBaidu(String word) throws 
                UnsupportedEncodingException,
                ClientProtocolException,
                IOException {
                //获取建议词
                String url="http://suggestion.baidu.com/su?wd="+URLEncoder.encode(word, "utf8");
                URL urlReal=new URL(url);
                InputStream stream=urlReal.openStream();
                BufferedReader streamReader=new BufferedReader(new InputStreamReader(stream,"utf-8"));
                StringBuffer stringBuffer=new StringBuffer();
                String temp=null;
                while((temp=streamReader.readLine())!=null){
                    stringBuffer.append(temp);
                }
                /*
                 * 这里的content总是会乱码啊*/
                String content=new String(stringBuffer.toString());
                System.out.println(content);
                String[] suggestions=content.replaceAll(".*\\[(.*)\\].*", "$1").replaceAll("\"","").split(",");
                return suggestions;
            }
        });
        this.listSuggestions.addListSelectionListener(e -> {
            textInput.setText(listSuggestions.getSelectedValue());
        });
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() ->{
            new SuggestionTest();
        });
    }
}
阅读 7.5k
3 个回答

两个utf-8换成GBK
图片描述

因为你的字符中有汉字出现,将字符编码换成GB18030,是中华人民共和国现时最新的内码字集。

具体是设置UTF-8还是GBK,与服务器端保持一致,虽然国内用GBK/GB18030多,但很多网站不只供国内人用,UTF8的也不少。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题