gpt4 book ai didi

java - 如何将 php 变量返回给 android 应用程序?

转载 作者:行者123 更新时间:2023-11-29 04:16:37 25 4
gpt4 key购买 nike

我是 Android Studio 编程的新手,我正在尝试将我的应用程序连接到 Web 服务器上的 MySQL 数据库,echo "login success !!!!! Welcome $user"; 是正如我所愿,工作正常,但我想得到一个像 $user 这样的 PHP 变量也回到我的 android studio 开发中,这样我就可以用它在应用程序中创建 session ,但我无法获取变量我得到了 echo ,我在其他地方使用它来显示登录成功。需要帮助谢谢

    <?php
require "pconn.php";
$username = $_POST["username"];
$password = $_POST["password"];

$username = stripslashes($username);
$password = stripslashes($password);

$hash = md5($password);

$qry = "SELECT * FROM UserLogin WHERE Email='".$username."' and BINARY Password ='".$hash."'";
$result = mysqli_query($conn ,$qry);
if(mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$user = $row['Username'] ;
echo "login success !!!!! Welcome $user";
echo $user;
}
else {
echo "login not success";
}


?>

我的 Java 类是:

protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://xaas.com/login.php";
if(type.equals("login")) {
try {
String username = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";

while((line = bufferedReader.readLine())!= null) {
result += line;
}



bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}

最佳答案

使用print用于向 php 控制台显示任何内容,而 echo 只是您要作为对 android 设备的响应发送的部分。

或者,如果您想发送 "login success !!!!! Welcome $user" 字符串作为响应。然后在android端简单地获取$user的值。

第一次执行 echo 时,将作为响应发送。如果你想发送多个字符串,那么构建一个 json,用你想发送的任何内容填充它,最后回显它。像这样:

$data = [ 'loginMessage' => 'login success !!!!! Welcome $user', 'user' => '$user' ];
header('Content-type: application/json');
echo json_encode( $data );

然后使用 JSONObject 在 android 端取回值:

JSONObject responseJSON = new JSONObject(result);
String loginMessage = responseJSON.getString("loginMessage");
String userName = responseJSON.getString("user");

关于java - 如何将 php 变量返回给 android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42176506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com