2013-07-07 86 views
0

我想從一個Java小程序發送兩個變量的值到一個PHP文件),並且我嘗試了下面的代碼。從applet傳遞值到PHP

try { 
URL url = new URL(getCodeBase(),"abc.php"); 
URLConnection con = url.openConnection(); 

con.setDoOutput(true); 
PrintStream ps = new PrintStream(con.getOutputStream()); 

ps.print("score="+score); 
ps.print("username="+username); 

con.getInputStream(); 

ps.close(); 
} catch (Exception e) { 
    g.drawString(""+e, 200,100); 
} 

我得到了以下錯誤:

您正在使用不支持輸出協議
java.net.UnknownServiceException:protocol doesn't support output 
+0

@ luk2302你是完全錯誤的。這是POST請求的工作方式。 – BackSlash

+0

艾姆,那......可悲......真的,早上太早了 - 我會刪除我的評論。 – luk2302

回答

0
java.net.UnknownServiceException:protocol doesn't support output 

手段。

getCodeBase()指文件的URL,所以像

file:/path/to/the/applet 

的協議是file,不支持outout。您正在尋找支持輸出的http協議。

也許你想getDocumentBase(),這實際上返回網頁裏的小程序,即

http://www.path.to/the/applet 
0

下面是一些代碼,我用我自己的小程序所使用,對值(通過POST)發送到PHP腳本我的服務器:

我會用這樣的:

String content = ""; 
    content = content + "a=update&gid=" + gid + "&map=" + getMapString(); 
    content = content + "&left_to_deploy=" + leftToDeploy + "&playerColor=" + playerColor; 
    content = content + "&uid=" + uid + "&player_won=" + didWin; 
    content = content + "&last_action=" + lastActionCode + "&appletID=" + appletID; 

    String result = ""; 
    try { 
    result = requestFromDB(content); 
    System.out.println("Sending - " + content); 
    } catch (Exception e) { 
    status = e.toString(); 
    } 

正如你所看到的,我加入了我所有的值發送到「內容」的字符串,然後再調用我requestFromDB方法(這帖子我的「請求」值,並返回服務器的響應):

public String requestFromDB(String request) throws Exception 
    { 
    // This will accept a formatted request string, send it to the 
    // PHP script, then collect the response and return it as a String. 
    URL     url; 
    URLConnection urlConn; 
    DataOutputStream printout; 
    DataInputStream  input; 
    // URL of CGI-Bin script.  
    url = new URL ("http://" + siteRoot + "/globalconquest/applet-update.php"); 

    // 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()); 
    printout.writeBytes (request); 
    printout.flush(); 
    printout.close(); 
    // Get response data. 
    input = new DataInputStream (urlConn.getInputStream()); 
    String str; 
    String a = ""; 
    while (null != ((str = input.readLine()))) 
    { 
     a = a + str; 
    } 

    input.close(); 
    System.out.println("Got " + a); 
    if (a.trim().equals("1")) { 
     // Error! 
     mode = "error"; 
    } 

    return a; 

    } // requestFromDB 

在我的PHP腳本,我只需要看看$ _ POST我的價值觀。然後我會打印一個回覆。

注意!出於安全原因,您的PHP腳本必須與applet位於同一臺服務器上,否則這將不起作用。