StreamReader将ReadLine保存为字符串,并在以下情况下使用

时间:2018-09-20 19:29:03

标签: c# webclient streamreader

嗨,我想将reader.ReadToEnd()的输出保存到字符串中,并检查字符串是否为“ Access”,但我不知道该怎么做。

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Log.i(TAG,"**********  onNotificationPosted");
    Log.i(TAG,"ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
    Intent i = new  Intent("com.example.readandroidnotification.NOTIFICATION_LISTENER_EXAMPLE");
    i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "n");
    sendBroadcast(i);
}

1 个答案:

答案 0 :(得分:1)

您正在读取流两次,而无需进行任何重置,因此建议仅读取一次。另外,您应该适当地处理流和streamreader。请参阅以下内容:

        string url = "https://mywebsite.com/check.php";
        string remoteData = null;
        using (Stream mystream = client.OpenRead(url))
        using (StreamReader reader = new StreamReader(mystream))
            remoteData = reader.ReadToEnd();
        Console.WriteLine(remoteData); //The text will be "Access"

        //Pseudecode start
        if (remoteData == "Access")
        {
            useraccess = true;
            Console.WriteLine("Done!");
        }

这应该在ReadToEnd()返回您希望它返回的内容的假设下进行。我不知道您的端点是什么样子,所以无法验证。