Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  HTTP POST requests  (Read 1383 times)
0 Members and 1 Guest are viewing this topic.
Offline trajedebano

Junior Newbie





« Posted 2006-02-09 16:41:21 »

Okay, so I've developed a simple tetris clone applet that I want to send score and user information to a PHP script after the game is over.

The PHP form then takes this information and submits it to a MySQL database (JDBC is not an option, I do not have remote access to the web server)

For some reason though, it doesn't seem to work. If I manually execute the PHP file, it works fine (it creates a log of all scores submitted so I can debug this), but when I try to use the Java applet it seems to connect but it does not do anything afterwards. My code looks like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
    
public void sendScores() {
        String[] stats;
        stats = Tetris.getGameStats();
        sentScores = true;
        URL url;
        URLConnection urlConn;
        DataOutputStream printout;
       
        try {
            String q = URLEncoder.encode("user_id", "UTF-8") + "=" + URLEncoder.encode(strUserId, "UTF-8");
            q += "&" + URLEncoder.encode("ruleset", "UTF-8") + "=" + URLEncoder.encode(strRuleSet, "UTF-8");
            q += "&" + URLEncoder.encode("gametype", "UTF-8") + "=" + URLEncoder.encode(strGameType, "UTF-8");
            q += "&" + URLEncoder.encode("score", "UTF-8") + "=" + URLEncoder.encode(stats[0], "UTF-8");
            q += "&" + URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(stats[1], "UTF-8");
            q += "&" + URLEncoder.encode("timelimit", "UTF-8") + "=" + URLEncoder.encode(strTimeLimit, "UTF-8");
            q += "&" + URLEncoder.encode("ruleset", "UTF-8") + "=" + URLEncoder.encode(strScoreLimit, "UTF-8");            
            System.out.println(q);
            url = new URL (getCodeBase().toString() + "submitScore.php");
            System.out.println(url.toString());
            urlConn = url.openConnection();
            urlConn.setDoOutput (true);
            urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            printout = new DataOutputStream (urlConn.getOutputStream ());
            printout.writeBytes(q);
            printout.flush();
            printout.close();
            System.out.println("sendScore() executed");
        } catch (Exception e) { }
       
    }


As you can see I followed Java World Tip #34 very closely.
The applet does NOT throw an exception, so it appears to have executed all the code correctly, but it doesn't appear to actually send the information to the script. There is very little out there beyond the javaworld site. If anyone wants to look at the php file I've attached it as well. It is supposed to create a log file even if no data is actually posted to the php script.
Offline woogley
« Reply #1 - Posted 2006-02-09 17:28:23 »

I can't quite tell what you're doing wrong but, if you're interested.. I wrote a highscore script for somebody a few months ago that worked for him very well. if you wanna use it I don't care.. no credit needed: http://woogley.net/misc/Highscore
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 9


Age of Conquest makes your day!


« Reply #2 - Posted 2006-02-10 20:57:26 »

You are sending with GET! But in your PHP file, you are reading from POST. There is nothing there in POST, because all your data is sent using GET.

GET: Uses the "?...&...&" in the URL.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 9


Age of Conquest makes your day!


« Reply #3 - Posted 2006-02-10 20:58:25 »

Also do not use:
1  
        } catch (Exception e) { }


At least put some text out:
1  
2  
3  
        } catch (Exception e) {
             System.out.println("Error encountered: " + e);
        }


Offline trajedebano

Junior Newbie





« Reply #4 - Posted 2006-02-11 07:42:38 »

oh, I didn't know I was. I'll try changing $_POST to $_GET and see what happens.

Still though, its not doing anything. At very minimum the PHP script should write to my log file even if it doesn't accept any variables. Unless I'm mistaken here?
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2006-02-12 02:49:01 »

As kingaschi pointed out...

Never ever ever ever catch an exception with an empty catch block.

You can have all kinds of errors you know nothing about because you are hiding them.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (57 views)
2014-04-15 18:08:23

BurntPizza (55 views)
2014-04-15 03:46:01

UprightPath (68 views)
2014-04-14 17:39:50

UprightPath (51 views)
2014-04-14 17:35:47

Porlus (68 views)
2014-04-14 15:48:38

tom_mai78101 (93 views)
2014-04-10 04:04:31

BurntPizza (153 views)
2014-04-08 23:06:04

tom_mai78101 (249 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (212 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!