【发布时间】: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.)。请逐字包含错误消息(除了考虑我之前的评论)。 -
谢谢你的建议,我会改变我的问题