2013-07-07 2 views
0

Java 애플릿에서 PHP 파일에 두 개의 변수 값을 보내고 싶습니다.) 다음 코드를 시도했습니다. 애플릿에서 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

Aehm, thats ... 슬프게도 ... 사실, 너무 일찍 아침에 .- 나는 내 의견을 삭제하겠습니다. – luk2302

답변

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

수단을.

getCodeBase()는 파일 URL을 참조, 그래서 뭔가

file:/path/to/the/applet 

같은 프로토콜은 outout 지원하지 않는, file이다. 출력을 지원하는 http 프로토콜을 찾고 있습니다.

아마 당신은 실제로 애플릿은 웹 페이지를 반환 getDocumentBase()을, 원 예

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

저는 여기에 PHP 스크립트 (POST를 통해) 값을 보내, 내 자신의 애플릿으로 사용되는 일부 코드는 내 서버 :

I는 다음과 같이 사용합니다 :

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 방법 :

내 PHP 스크립트에서
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 

, 난 단지 내 값을 $ _POST 볼 필요가있다. 그런 다음 응답을 인쇄합니다.

참고! 보안상의 이유로 PHP 스크립트가 애플릿과 동일한 서버에 있어야합니다. 그렇지 않으면 작동하지 않습니다.