Sign in to follow this  
Setantaa

[java] Getting a java applet to call php scripts?

Recommended Posts

Hi Guys, I used an Applet to make a racing car game. The game works pretty well. I want to now integrate it with Facebook and will need to call some of there API's. I am slightly familiar with PHP. Facebook supports 2 languages for there API officially. PHP and javascript. There is a java API but its not official and I dnt know much about it. So to the question.(Guess its more of a design problem) Can applets work effectively to pass and receive data from php scripts. The php scripts will then interface with Facebook servers and my database. If there were 50 users playing the applet and calling these php scripts on the server should it be able to handle it? Would the design that I am thinking about seem feasible? Its my first attempt at web programming so apologies if I am way off. Thanks Set

Share this post


Link to post
Share on other sites
I did something like this a long time ago, but it was pretty primitive. Basically the applet would attempt to open a local web-page with all of the data being passed in the url, and the php script would return a "web page" which contained data in an ascii format. There might be a cleaner way to do it, but that works. Here's the java code. Sorry about the formatting (or lack thereof).


public String connect(String page, String vars[], String values[]){

String output="" ;

try{


URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
// URL of CGI-Bin script.
url = new URL (getCodeBase().toString() + page);

// URL connection channel.
urlConn = url.openConnection();
// Let the run-time system (RTS) know that we want input.
urlConn.setDoInput (true);
// Let the RTS know that we want to do output.
urlConn.setDoOutput (true);
// No caching, we want the real thing.
urlConn.setUseCaches (false);
// Specify the content type.
urlConn.setRequestProperty

("Content-Type", "application/x-www-form-urlencoded");
// Send POST output.
printout = new DataOutputStream (urlConn.getOutputStream ());
String content = "" ;
for(int k=0;k<vars.length;k++){
content+=URLEncoder.encode (vars[k])+"="+URLEncoder.encode (values[k]) ;
content+="&" ;
}

printout.writeBytes (content);
printout.flush ();
printout.close ();
// Get response data.
input = new DataInputStream (urlConn.getInputStream ());
String str;
while (null != ((str = input.readLine())))

{

output+=str+"\n";

}



input.close ();


System.out.println("score sent successfully!") ;
}catch(Exception e){
System.out.println("Connection failed due to error");
System.out.println(e) ;


}

return output ;

}


Share this post


Link to post
Share on other sites
Hi Alrecenk,

Is there not some standard feature/api of java for handling interfacing with php scripts? Since it seems you had to hack away around this. If people dnt use php what are people using instead?

Share this post


Link to post
Share on other sites
Quote:
Is there not some standard feature/api of java for handling interfacing with php scripts? Since it seems you had to hack away around this. If people dnt use php what are people using instead?


"Interfacing with PHP scripts" is a matter of sending HTTP requests to a web server. What you could do is create a submit_score.php script on your web server. Contruct a web request as Alrecenk has demonstrated and call your submit_score.php with whatever parameters you like. submit_score.php will update your database and can additionally do whatever "Facebook" stuff you want it to do by using the PHP API.

Quote:
If there were 50 users playing the applet and calling these php scripts on the server should it be able to handle it? Would the design that I am thinking about seem feasible?


It depends on how many requests they are sending to the web server and the capacity of the web server. Remember, the applet is running on the client's computer, so once they have downloaded it, they are not generating any load on the server. If you are just sending high score info, then the only time you would be generating web traffic would be when they actually submit a score, which I assume would be at "Game Over" which should not be happening that often per client.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this