如何从php中调用的java文件中获取返回值

时间:2017-03-20 05:50:43

标签: java php web-services rest

我在PHP中使用HttpURLConnection来调用Java文件。在JAVA中,如何返回字段内容(String),在PHP中如何接收它们?谢谢。对任何可以提供帮助的人都有很棒的意见。洛尔。

示例代码:

爪哇:

   @Controller
@RequestMapping("/rest")
public class ProjectController {


    @RequestMapping(value = "/ediresponceback", method = RequestMethod.POST)
    public String ediResponceBack(HttpServletRequest webRequest) throws FileNotFoundException, IOException {
        ParseEDI271 myParser = new ParseEDI271();
        if (webRequest.getParameter("edistring") != null) {
            myParser.setEDI271(webRequest.getParameter("edistring"));
        } else {
            myParser.setEDI271("Responce not found");
        }
        try {
            myParser.parseEDI();
        } catch (FormatException e) {
            e.printStackTrace();
        }

        String ediTextMessage = myParser.getEdiTextMessage();

        return ediTextMessage;
    }

}file?
        con.disconnect();
    }

PHP:

  <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>

<!--     
    http://phpfiddle.org
-->

<script type="text/javascript">

</script>

<style type="text/css">

</style>
</head>

<body>
<div style="margin: 30px 10%;">
<h3>My form</h3>
<form id="myform" name="myform" action="http://localhost:8080/EDI271Parser/rest/ediresponceback/" method="post">


    <input type="hidden" name="edistring" value="ISA|00|          |00|          |ZZ|ZIRMED         |ZZ|113394         |170307|0309|}|00501|000376343|0|P|^~GS|HB|ZIRMED|113394|20170307|0309|376335|X|005010X279A1~ST|271|0001|005010X279A1~BHT|0022|11|372490|20170307|030929~HL|1||20|1~NM1|PR|2|ALLSAVERS INSURANCE COMPANY|||||PI|AMMEDS~PER|IC|CUSTOMER SERVICE|TE|8002325432~HL|2|1|21|1~NM1|1P|2|LIFEBRITE LABORATORIES, LLC|||||XX|1609273317~HL|3|2|22|0~TRN|2|070313391|3030240928~TRN|2|549271659|9ZIRMEDCOM|ELR ID~TRN|1|540911230|9ZIRMEDCOM|ELI ID~NM1|IL|1|ALEXANDER|ARTHUR|L|||MI|005267029~N3|13180 COOPER CT~N4|HAMPTON|GA|30228~DMG|D8|19700506|M~DTP|346|D8|20160101~EB|1|IND|30||EPO E300030~MSG|INFORMATION PROVIDED IN A 271 IS NOT A GUARANTEE OF PAYMENT OR COVERAGE IN ANY SPECIFIC AMOUNT. ACTUAL BENEFITS DEPEND ON VARIOUS FACTORS, INCLUDING COMPLIANCE WITH APPLICABLE STATE REQUIREMENTS; DATE(S) OF SERVICES RENDERED AND BENEFIT PLAN TERMS AND CONDITIONS.~EB|1|IND|1}33}47}48}50}86}98}MH}UC||EPO E300030||||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|1|IND|47}48}50}86||EPO E300030||||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|I|IND|35}AL||EPO E300030||||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|I|IND|1}33}35}98}AL}MH}UC||EPO E300030||||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|U|IND|88||0 DED-15/35/75/250 5500||||||U~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~LS|2120~NM1|VN|2|OptumRx~LE|2120~EB|A|IND|1}33}47}48}50}86}98}MH}UC||EPO E300030|||0|||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|A|IND|47}48}50}86||EPO E300030|||0|||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|1}98}MH||EPO E300030||30||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|33}47}48}50||EPO E300030||0||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|86||EPO E300030||300||||U~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|UC||EPO E300030||100||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|B|IND|47}48}50||EPO E300030||0||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|1}33}47}48}50}86}98}MH}UC||EPO E300030|27|0||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|47}48}50}86||EPO E300030|27|0||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|1}33}47}48}50}86}98}MH}UC||EPO E300030|29|0||||U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|47}48}50}86||EPO E300030|29|0||||U|N~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|F|IND|33||EPO E300030|23|||VS|30|U|Y~MSG|SERVICES MAY REQUIRE REFERRAL AND/OR PRIOR AUTHORIZATION.~EB|C|IND|30||EPO E300030|23|3000||||N|Y~EB|C|IND|30||EPO E300030|23|0||||N|N~EB|C|IND|30||EPO E300030|29|3000||||N|Y~EB|C|IND|30"
                + "||EPO E300030|29|0||||N|N~EB|G|IND|30||EPO E300030|29|5500||||N|Y~EB|G|IND|30||EPO E300030|29|0||||N|N~SE|62|0001~GE|1|376335~IEA|1|000376343~" /> 


    <button id="mysubmit" type="submit">Submit</button><br /><br />

</form>
</div>

<?php

?>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

我最近竞争同样的事情。我花了一段时间才弄明白,但这才是最终奏效的。

JS FILE (AJAX Request) --> PHP SHELL SCRIPT --> JS FILE 我将它们称为A,B和C

A看起来像这样:

function verify(){

        $.ajax({
        url:"scripts/shell.php", //the page containing php script
        type: "post", //request type,
        dataType: 'json',
        data: {data1: "hi", data2: "hello", data3: "bye"}, //OPTIONAL, Used as arguments for js file down the road. 
        success:function(result){
         console.log(result.abc);
       }
     });
    }

非常标准的AJAX请求

B看起来像这样:

<?php  
$EH= $_POST['data1'];
$EHH = $_POST['data2'];
$EHHH = $_POST['data3'];

echo json_encode(array("abc" => shell_exec("...PATH-TO-JS/file.js 2>&1 $EH $EHHH $EHHH")));
?>

通知此shell脚本echoencodes结果返回到A末尾提到的数组abc < / p>

C看起来像这样:

var system = require('system')
var H = system.args[1]
var Hi = system.args[2]
var Hii = system.args[3]
if(1 > 2){
 console.log("Passing Result Back") // OR true
}

然后将该结果传递回shell,shell将编码并传递回调用它的原始js文件。

希望这会有所帮助。

答案 1 :(得分:0)

以下是用于将String发送到Java应用程序的PHP代码,它将以字符串格式获得响应:

<?php

// The data to send to the API
$postData = array(
    'data' => 'Test'
);
//echo json_encode($postData);

// Create the context for the request
$context = stream_context_create(array(
    'http' => array(

        'method' => 'POST',
        'header' => "Content-Type: application/json\r\n",
        'content' => json_encode($postData)
    )
));


// Send the request
$response = file_get_contents('http://localhost:8080/EDI271Parser/rest/ediresponceback/', FALSE, $context);

// Check for errors
if($response === FALSE){
    die('Error');
}


// Print the date from the response
echo $response;

?>