如何将php变量恢复到Android应用程序?

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

我是Android Studio编程的新手,我正在尝试将我的应用程序连接到Web服务器上的MySQL数据库,echo "login success !!!!! Welcome $user";正如我所希望的那样正常工作,但我想得到像$user这样的PHP变量也回到我的android工作室开发,以便我可以使用它在应用程序中进行会话,但我无法得到变量我得到回声,我在其他地方使用它喜欢显示登录成功。需要帮助,谢谢

    <?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;
    }
java php android mysql
3个回答
1
投票

使用printphp控制台显示任何内容,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");

2
投票

就像上面的所有答案一样,你需要将你的php输出作为json回显。为此,您可以在php文件中尝试以下代码

$result["sessionID"] = $user;
$result["response_message"] = "Log in success!"
echo json_encode($result);

现在你必须解析OutputStream的结果,为此,你可以尝试android类中的followin代码

JSONObject jsonobj = new JSONObject([your variables that holds the result from output stream]);
String sessionID = jsonobj.getString("sessionID");
String response_message = jsonobj.getString("response_message");

请注意,传递给jsonobj.getString(“sessionID”)的参数应该与在php文件中声明变量$ response [“sessionID”]时传递的参数相同。

我还建议您熟悉JSON语法。


1
投票

JSON和XML都是将数据从Web服务器返回到移动应用程序的常用方法。

或者,直接在Android EditTexts中输入用户名和密码,并将数据发送到PHP脚本。请注意,您使用的是HTTPS而不是普通的HTTP。

© www.soinside.com 2019 - 2024. All rights reserved.