Skip to content

How to Encode An Object to JSON with GSON

		

Gson gson = new Gson();
String str = gson.toJson(Object);

How to see Errors on Catalina

If you deploy your tomcat project on a server, the only way to see errors on your project is to check Catalina on the server.

First go to tomcat logs directory on your server

This path can be:

usr / local / tomcat / logs

In log directory

write the command tail catalina.out

Yes, this is it! Now you can see all the errors

How to Decode JSON with Google GSON Library

String data ="[ { \"away\": \"Ankaragücü\", \"date\": \" FT - 21/03/11\", \"home\": \"Bursaspor\", \"score\": \"0:0 \" }, { \"away\": \"Sivasspor\", \"date\": \" FT - 20/03/11\", \"home\": \"Bucaspor\", \"score\": \"0:4 (0:1) \" }, { \"away\": \"Trabzonspor\", \"date\": \" FT - 20/03/11\", \"home\": \"Gençlerbirligi\", \"score\": \"1:2 (1:0) \" }, { \"away\": \"Karabukspor\", \"date\": \" FT - 20/03/11\", \"home\": \"Eskisehirspor\", \"score\": \"1:0 (1:0) \" }, { \"away\": \"Kayserispor\", \"date\": \" FT - 19/03/11\", \"home\": \"Besiktas\", \"score\": \"4:2 (0:1) \" } ]";
    
	  Gson gson = new Gson();
	  
	  Type type = new TypeToken<List<Table>>(){}.getType();
	  List<Table> table = gson.fromJson(data, type);

      for (Table tab : table) {
          System.out.println("Away Team:  " + tab.getAway());
      }
    }
public class Table {
String away;
String date;
String home;
String score;

public String getAway() {
	return away;
}
public void setAway(String away) {
	this.away = away;
}
public String getDate() {
	return date;
}
public void setDate(String date) {
	this.date = date;
}
public String getHome() {
	return home;
}
public void setHome(String home) {
	this.home = home;
}
public String getScore() {
	return score;
}
public void setScore(String score) {
	this.score = score;
}

}

How to Update Status With Twitter API

Twitter twitter = new TwitterFactory().getInstance();
AccessToken accessToken = new AccessToken("token","secret");
twitter.setOAuthConsumer("oAuthToken", "oAuthSecret");
twitter.setOAuthAccessToken(accessToken);
Status status = twitter.updateStatus("This is My Tweet");

How to Get Twitter Token And Token Secret

import java.io.BufferedReader;
import java.io.InputStreamReader;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
public class UpdateStatus {
  /**
   * @param args
   */
  public static void main(String args[]) throws Exception {
	  
	  
	  // The factory instance is re-useable and thread safe.
      Twitter twitter = new TwitterFactory().getInstance();

//insert the appropriate consumer key and consumer secret here

      twitter.setOAuthConsumer(token, secret);
      RequestToken requestToken = twitter.getOAuthRequestToken();
      AccessToken accessToken = null;
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      while (null == accessToken) {
        System.out.println("Open the following URL and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
        String pin = br.readLine();
        try{
          
          
           if(pin.length() > 0){
             accessToken = twitter.getOAuthAccessToken(requestToken, pin);
           }else{
             accessToken = twitter.getOAuthAccessToken();
           }
        } catch (TwitterException te) {
          if(401 == te.getStatusCode()){
            System.out.println("Unable to get the access token.");
          }else{
            te.printStackTrace();
          }
        }
      }
      //persist to the accessToken for future reference.
      System.out.println(twitter.verifyCredentials().getId());
      System.out.println("token : " + accessToken.getToken());
      System.out.println("tokenSecret : " + accessToken.getTokenSecret());
      //storeAccessToken(twitter.verifyCredentials().getId() , accessToken);
      Status status = twitter.updateStatus("This is my Twitter Status");
      System.out.println("Successfully updated the status to [" + status.getText() + "].");
      System.exit(0);
    }
}

How to Set a Proxy for Java Applications

System.setProperty("http.proxyHost", "xx.xx.x.x");
System.setProperty("http.proxyPort", "xxxx");

How to Print an InputStream

InputStream in;
String readLine;
BufferedReader br = new BufferedReader(new InputStreamReader(in));

while (((readLine = br.readLine()) != null)) {
System.out.println(readLine);


}