请参阅以下代码。当我在""
中使用php
并被发送到android时,可以运行以下条件:
// Show response on activity
String text2 = "";
boolean check = text.equals(text2);
if(check == true)
{ String display;
display = (String)getText(R.string.display);
Toast.makeText(getApplicationContext(), display, Toast.LENGTH_LONG).show();}
else {textView.setText(text);}
和
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$username = urldecode($_POST['username']);
$login = urldecode($_POST['login']);
if(empty($username) || empty($login)) {
$display = "";
echo $display;
}}
在上面的代码中,当我使用$display = "";
时,将空格字符发送到text
对象,因为text2 = ""
Toast类也会显示R.string.display
。
但是当我使用text
之类的字符或其他不像空格字符(""
)的字符时,$username
或$login
是为空,在以下代码中,Toast类无法工作,而else {textView.setText(text);}
可以正常工作。
// Show response on activity
String text2 = "text";
boolean check = text.equals(text2);
if(check == true)
{ String display;
display = (String)getText(R.string.display);
Toast.makeText(getApplicationContext(), display, Toast.LENGTH_LONG).show();}
else {textView.setText(text);}
和
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$username = urldecode($_POST['username']);
$login = urldecode($_POST['login']);
if(empty($username) || empty($login)) {
$is = "text";
echo $is;
}}
?>
全功能表格
public void GetText() throws UnsupportedEncodingException
{
// Get user defined values
name = username.getText().toString();
pass = password.getText().toString();
// Create data variable for sent values to server
String data = URLEncoder.encode("username", "UTF-8")
+ "=" + URLEncoder.encode(name, "UTF-8");
data += "&" + URLEncoder.encode("login", "UTF-8") + "="
+ URLEncoder.encode(pass, "UTF-8");
String text = "";
BufferedReader reader=null;
// Send data
try
{
// Defined URL where to send data
URL url = new URL("http://127.0.0.1:8080/apps/example.php");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
}
catch(Exception ex)
{
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {}
}
// Show response on activity
String text2 = "text";
boolean check = text.equals(text2);
if(check == true)
{ String display;
display = (String)getText(R.string.display);
Toast.makeText(getApplicationContext(), display, Toast.LENGTH_LONG).show();}
else {textView.setText(text);}
}}
答案 0 :(得分:0)
在你的while循环中,你将\n
附加到每一行。这意味着,当您阅读字符串"text"
时,它实际上会在"text\n"
中读取。只需将您要比较的字符串更改为"text\n"
,它就可以正常工作。
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
...
// Show response on activity
String text2 = "text\n";