如何向Java发出http请求(并处理响应)

时间:2019-10-29 11:09:11

标签: java php get send

我在使用Java中的应用程序时遇到了麻烦

我必须使用我的android应用程序将一些信息(其中的键)发送到PHP文件(register.php)。 然后使用PHP脚本中的密钥生成一个密码。 我的Java应用程序获得了密码。

首先,我尝试将Java密钥传递给我的PHP并获取它。

但是当我这样做时,我会收到以下结果:

 <br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> 
Notice: Use of undefined constant FirstName - assumed 'FirstName' in C:\wamp64\www\Register.php on line <i>16</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>367320</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\Register.php' bgcolor='#eeeeec'>...\Register.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant LastName - assumed 'LastName' in C:\wamp64\www\Register.php on line <i>17</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>367320</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\Register.php' bgcolor='#eeeeec'>...\Register.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant Reason - assumed 'Reason' in C:\wamp64\www\Register.php on line <i>18</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>367320</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\Register.php' bgcolor='#eeeeec'>...\Register.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant Key - assumed 'Key' in C:\wamp64\www\Register.php on line <i>19</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>367320</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\Register.php' bgcolor='#eeeeec'>...\Register.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0

我的程序接受了PHP页面的所有回显,并且我知道出了什么问题。读者成功读取了所有回声线,但它读取了此enter image description here

在将信息发布到PHP的时间与从PHP获取信息的时间之间,信息会丢失。也许是因为我进入获取页面时页面正在充电。

这是我的代码Java


Context context;
        BackgroundTask(Context ctx)
        {
            this.context = ctx;
        }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

protected String Password;

public String getpassword()
{
    return Password;
}


@Override
protected String doInBackground(String... params) {
        String RegistrationUrl = "http://myip:port/register.php";
    String method = params[0];
    String FirstName = params[1];
    String LastName = params[2];
    String Reason = params[3];
    String Key = params[4];






    try {
        URL url = new URL(RegistrationUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        OutputStream Os = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(Os, "UTF-8"));
        String Data = URLEncoder.encode("FirstName", "UTF-8") + "=" + URLEncoder.encode(FirstName, "UTF-8") + "&"
                + URLEncoder.encode("LastName", "UTF-8") + "=" + URLEncoder.encode(LastName, "UTF-8") + "&"
                + URLEncoder.encode("Reason", "UTF-8") + "=" + URLEncoder.encode(Reason, "UTF-8") + "&"
                + URLEncoder.encode("Key", "UTF-8") + "=" + URLEncoder.encode(Key, "UTF-8");

        bufferedWriter.write(Data);
        InputStream IS = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

        String line;
        String result ="";
        while ((line = bufferedReader.readLine()) != null)
        {
            result = result + line;
        }
       Password = result;
        bufferedWriter.flush();
        bufferedWriter.close();
        Os.close();
     bufferedReader.close();
      IS.close();
      httpURLConnection.disconnect();

    } catch (MalformedURLException e) {
        e.printStackTrace();

    } catch (ProtocolException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();

    }

return Password;
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}


@Override
protected void onPostExecute(String result) {
    Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}

还有我的PHP代码


    <?php require "connection.php";

$FirstName = $_POST[FirstName];
 $LastName = $_POST[LastName];
 $Reason = $_POST[Reason];
 $Key = $_POST[Key];

$mysql_qry = "INSERT INTO requesttable(FirstName, LastName, Reason) VALUES ('$FirstName', '$LastName', '$reason')";


if($conn->query($mysql_qry) === TRUE)
{
    echo $Key;
}
else
{
    echo "error: ".$mysql_qry ."<br>". $conn->error; 
}
?>

我在网上进行搜索,但是关于它我却找不到很多。我在使用Java和PHP时遇到一些困难

感谢您的帮助

1 个答案:

答案 0 :(得分:-1)

我将那些$ _POST ['variableNames']用单引号或双引号引起来,因为您使用的是常量FirstName,LastName等,这些常量尚未预定义,这就是为什么$ _POST值丢失的原因,因为它们是没有分配给php变量。

$FirstName = "";
if(isset($_POST['FirstName']){
$FirstName = $_POST['FirstName'];
} else {
 //some errror
}

否则,要使用我似乎不必要先定义这些常量的常量:

define("FirstName", "FirstName");
$FirstName = $_POST[FirstName];

为防止php响应出现错误/警告,请在php页面顶部使用error_reporting(0)(例如,如果php提供了对特定功能的输出等)