Java tutorial
package com.yoshio3.javaee7.sampleapp; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.websocket.Session; import twitter4j.FilterQuery; import twitter4j.Status; import twitter4j.StatusAdapter; import twitter4j.TwitterStream; import twitter4j.TwitterStreamFactory; import twitter4j.User; /** * * @author tyoshio2002 */ public class TwitterStreamImpl extends StatusAdapter { private TwitterStream twStream = null; private Session session; public void initTwitterStream(Session session, String filterString) { this.session = session; //Twitter Stream ?? twStream = TwitterStreamFactory.getSingleton(); FilterQuery filter = new FilterQuery(); filter.track(new String[] { filterString }); filter.language(new String[] { "ja" }); twStream.addListener(this); twStream.filter(filter); } @Override public void onStatus(Status status) { try { //Twitter ???????? User user = status.getUser(); String resStr = "@" + user.getScreenName() + " : " + status.getText(); String encodedString = encode(resStr); session.getBasicRemote().sendText(encodedString); System.out.println(encodedString); } catch (IOException ex) { Logger.getLogger(TwitterStreamImpl.class.getName()).log(Level.SEVERE, null, ex); } } /** * HTML??? * */ static char[] htmlEncChar = { '&', '"', '<', '>' }; /** * HTML?? * */ static String[] htmlEncStr = { "&", """, "<", ">" }; /** * HTML? &,",<,>?? * */ public String encode(String strIn) { if (strIn == null) { return (null); } // HTML? StringBuffer strOut = new StringBuffer(strIn); // ????? for (int i = 0; i < htmlEncChar.length; i++) { // ???? int idx = strOut.toString().indexOf(htmlEncChar[i]); while (idx != -1) { // ????? strOut.setCharAt(idx, htmlEncStr[i].charAt(0)); strOut.insert(idx + 1, htmlEncStr[i].substring(1)); // ????? idx = idx + htmlEncStr[i].length(); idx = strOut.toString().indexOf(htmlEncChar[i], idx); } } return (strOut.toString()); } public void destroyStream() { twStream.removeListener(this); twStream.shutdown(); } }