[用Java发送数据到php并获取转换后的数据

问题描述 投票:0回答:1

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

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

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

但是当我这样做时,我收到以下结果:“ .....警告:使用未定义的内容...”

我的程序接受了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时遇到一些困难

感谢您的帮助

java php get send
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.