【问题标题】:how to emit a http request for java (and handle the response)如何为 java 发出 http 请求(并处理响应)
【发布时间】:2020-02-24 15:00:21
【问题描述】:

我在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 获取信息之间,信息都丢失了。可能是因为我去get时页面正在收费。

这是我的 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 时遇到了一些困难

感谢您的帮助

【问题讨论】:

  • 首先,请阅读How to Ask。我们希望您直接在问题中以文本形式显示与您的问题相关的所有代码,格式正确 - 不要只显示 of 代码的图像。
  • 哦...好吧,对不起,第一次。
  • 请认识到您似乎有 2 个问题 - 1)如何为 java 发出 http 请求(并处理响应) 2)如何接受发布请求(并生成响应)。如果您将它们视为字面上的 2 个问题,您将获得更大的成功 - 并从第二个问题开始。我敢肯定这两个问题之前都被问过很多次了。
  • 问题仍然引用了代码即图像和解释错误消息 (use of undefined content.)。请逐字包含错误消息(除了考虑我之前的评论)。
  • 谢谢你的建议,我会改变我的问题

标签: java php get send


【解决方案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 为 deprericated 函数等提供输出)

【讨论】:

  • To prevent errors/warnings from php response use error_reporting(0) at top of php page - 这是一个糟糕的建议,尤其是在问题的上下文中 - 正确的行动是解决警告的原因。
  • 当然你必须首先修复错误 - 但是,主要问题是为什么会发生这种情况(出现警告) - 在我看来,我用提供的代码和解释回答了它,更多的我给出了一个提示如何防止 php 输出警告,例如,如果某些功能将在未来的 php 版本中被贬低 - 如果有什么要说的,你应该说出来,而不是这样......第二,那么也许我们应该开始用“你必须解决错误/警告的原因,然后一切都会好的人”来回答所有堆栈问题。 - 如果这是您对作者的建议! :D
  • Of course you have to fix error in first place - 请编辑答案以实际说出错误的含义,显示并解释如何避免这种情况,并且只有在您希望添加任何其他有用的提示时才这样做。答案目前隐含在代码中 - 使用单词使其明确:)。
  • 感谢您的回答。我使用post isset。但奇怪的是,我仍然有错误消息(只有应用程序,当我直接启动 php 时我什么都没有)。
  • 另外,问题不是错误信息。出现错误消息,因为我的数据在两次操作之间被清除。因此,如果发送密钥以获取密码: - 我的应用程序将发送密钥 - Php 将生成密码 - 我的应用程序将调用 php....并清除所有 - 我的应用程序将采用....一个空密码。这就是为什么我有一个错误“未定义的变量”,当我从 php 获取值时,我想保留我发送的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
  • 2013-06-22
  • 2011-02-23
  • 2011-07-18
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
相关资源
最近更新 更多