通过android从PHP读取字符串返回'null'

时间:2018-12-20 12:52:25

标签: php android

我是Android的新手。我读了许多有关该主题的文章,但没有帮助。

我已经创建了一个用于检查登录状态的PHP页面:

<?php
require_once 'wp-load.php'; 
global $current_user;
$current_user = wp_get_current_user();
//var_dump($current_user->ID);
//var_dump($current_user->display_name);
//var_dump($_COOKIE);

$newURL = "";

if ( !function_exists( 'is_user_logged_in' ) ) { 
    require_once 'wp-includes/pluggable.php'; 
} 

    $current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
    $newURL = "no";
    echo $newURL;
} else {
    // Logged in.
    $newURL = "si";
    echo $newURL;
}       
?>

然后我在Android中读取了字符串:

        try {
            url3 = new URL("https://example.com/lg.php");
            is = url3.openStream(); // throws an IOException
            br = new BufferedReader(new InputStreamReader(is));

            while ((line = br.readLine()) != null) {
                System.out.println(line);
                Log.d("YourTag", line);
            }
        } catch (Exception mue) {
            mue.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }

                            // Menu part 2
    nv = (NavigationView)findViewById(R.id.nv);

    if (line == "no")  {
        nv.getMenu().findItem(R.id.login).setVisible(true);
        nv.getMenu().findItem(R.id.logout).setVisible(false);
        Toast.makeText(context,"No.",Toast.LENGTH_LONG).show();
    } else if (line == "si") {
        nv.getMenu().findItem(R.id.login).setVisible(false);
        nv.getMenu().findItem(R.id.logout).setVisible(true);
        Toast.makeText(context,"Si.",Toast.LENGTH_LONG).show();
    }

问题是line变量为null,因此if不起作用。 有什么想法吗?

0 个答案:

没有答案