在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ Java 里StringBuffer.toString總是亂碼

Java 里StringBuffer.toString總是亂碼

各位好,這是我在網(wǎng)上看到的一個(gè)輸入框提示的程序,我改了一下,content總是亂碼,哪里的編碼出問題了呢?我試著把content裝成bytes[]在轉(zhuǎn)成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總是會(huì)亂碼啊*/
                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();
        });
    }
}
回答
編輯回答
冷眸

兩個(gè)utf-8換成GBK
圖片描述

2018年2月7日 20:23
編輯回答
久礙你

具體是設(shè)置UTF-8還是GBK,與服務(wù)器端保持一致,雖然國(guó)內(nèi)用GBK/GB18030多,但很多網(wǎng)站不只供國(guó)內(nèi)人用,UTF8的也不少。

2017年11月8日 11:07
編輯回答
夕顏

因?yàn)槟愕淖址杏袧h字出現(xiàn),將字符編碼換成GB18030,是中華人民共和國(guó)現(xiàn)時(shí)最新的內(nèi)碼字集。

2017年6月24日 04:22