Implement SSL to use twitter4j (switch from HTTP to HTTPS)
Hi ,
Im doing a project with twitter by collecting all tweets with a keyword using a java program.
It was working fine till sep 29 and now ssl have been implemented in it .
But im not much aware of how to use ssl.
could anyone help me out how to change my code to run with ssl.
while i tried to run my code it is giving statuscode =-1.
here is my code and i want to know where i should edit and make it work..
Pls provide me some suggestions.
Im using twitter4j-core-2.1.3;
Here is my code which is in java.
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import twitter4j.*;
public final class TwitterInformationStream extends StatusAdapter{
int count=1;
public static void main(String[] args)throws TwitterException{
String args1[] = new String[4];
args1[0]="twitteruserid"; args1[1]="pwd"; args1[2]="0"; args1[3]="dell laptop";
TwitterInformationStream tis = new TwitterInformationStream(args1);
tis.startConsuming();
}
TwitterStream twitterStream;
int[] filterArray;
String[] trackArray;
private TwitterInformationStream(String[] args)
{
String filter, track;
try {
if (args.length < 4)
{
printUsageAndExit();
}
twitterStream = new TwitterStreamFactory().getInstance(args[0], args[1]);
filter = args[2];
track = args[3];
}
catch (IllegalStateException is)
{
if (args.length < 4)
{
printUsageAndExit();
}
twitterStream = new TwitterStreamFactory().getInstance(args[0], args[1]);
filter = args[2];
track = args[3];
}
String[] filterSplit = filter.split(",");
filterArray = new int[filterSplit.length];
for(int i=0; i< filterSplit.length; i++)
{
filterArray[i] = Integer.parseInt(filterSplit[i]);
}
trackArray = track.split(",");
}
private void printUsageAndExit()
{
System.out.println("Usage: java twitter4j.examples.PrintFilterStream [ScreenName Password] follow(comma separated user ids) track(comma separated filter terms)");
System.exit(-1);
}
private void startConsuming() throws TwitterException
{
twitterStream.setStatusListener(this);
twitterStream.filter(0,filterArray,trackArray);
}
public void onStatus(Status status)
{
twitter4j.User user = (twitter4j.User)status.getUser();
Date createddate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String _server = "localhost:3306";
String _user = "root";
String _password = "pwdmysql";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
Statement stmt1 = null;
long tweetsid = 0;
try
{
ArrayList influencedhandles = new ArrayList();
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://"+_server+"/MyEmail?user="+_user+"&password="+_password);
stmt = conn.createStatement ();
stmt1 = conn.createStatement ();
int influencedhandleflag = 0;
String tweets = status.getText();
createddate = status.getCreatedAt();
tweetsid = status.getId();
String twittername = user.getName();
String twitterhandle = user.getScreenName();
String profileimageurl= user.getProfileImageURL().toString();
long tweetscount = user.getStatusesCount();
long followers = user.getFollowersCount();
long twitterid = user.getId();
String language = user.getLang();
long adoberank = Math.round(((followers * 0.3) + (tweetscount* 0.75)));
if(influencedhandles.contains(twitterhandle))
influencedhandleflag = 1;
int adobestars = 0;
int adobemediarank = 0;
int postweight = 1;
String lastrankeddt = "0000-00-00 00:00:00";
rs = stmt.executeQuery("SELECT SM_ID FROM MyAdobeInformation WHERE MediaType=1 AND SM_ID="+tweetsid);
if(rs.next())
{
System.out.println("Already Exist");
}
else if(language!=null && language.equals("en"))
{
stmt.executeUpdate(" INSERT INTO MyAdobeInformation (SM_ID, SMUser_ID, SMHandle, Post, ProfileImageURL, Created_DT,Insertion_DT,InfluencedHandleFlag,Followers,TweetsCount,AdobeRank,MediaType,AdobeStars,PostCount,LastRanked_DT) VALUES ("+tweetsid+","+twitterid+",'"+twitterhandle+"','"+tweets.replace("\'","\\\'")+"','"+profileimageurl+"','"+sdf.format(createddate)+"',NOW(),'"+influencedhandleflag+"',"+followers+","+tweetscount+","+adobemediarank+",1,"+adobestars+","+postweight+",'"+lastrankeddt+"')");
System.out.println("One data added");
System.out.println("Count="+count);
count++;
}
}
catch (Exception te)
{
System.out.println("Couldn't connect: " + te);
}
finally
{
if( rs!=null)
{
try { rs.close();} catch(SQLException sqlex) { }
rs=null;
}
if( stmt!=null)
{
try { stmt.close(); } catch(SQLException sqlex) { }
stmt=null;
}
if( stmt1!=null)
{
try { stmt1.close(); } catch(SQLException sqlex) { }
stmt1=null;
}
if( conn !=null)
{
try { conn.close(); } catch(SQLException sqlex) { }
conn=null;
}
}
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice)
{
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses)
{
}
public void onException(Exception ex)
{
ex.printStackTrace();
}
}
Could anyone help me to make it run again..
Thanks in advance,
Regards,
Siva.