首页 > 解决方案 > 通过 PHP 从 android 读取字符串返回 'null'

问题描述

我是 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变量为空,因此if不起作用。有任何想法吗?

标签: phpandroid

解决方案


推荐阅读