» 首頁 » 討論區 » Android程式設計 »If判斷一直錯誤

If判斷一直錯誤

發表人: nobb
積分: 58
發表時間: 2011-09-15 16:08:10
public void onClick(View v) {

// TODO Auto-generated method stub
HttpPost httpRequest = new HttpPost("http://140.137.61.89/WebForm1.aspx");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username",un.getText().toString()));
nvps.add(new BasicNameValuePair("password",pw.getText().toString()));
try
{
httpRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);

if(httpResponse.getStatusLine().getStatusCode()==200)
{
String result= EntityUtils.toString(httpResponse.getEntity());
result = result.trim().toLowerCase();
tv1.setText(result);
if(result.equals("true"))
{
error.setText("Correct Username or Password");
}
else
{
error.setText("Sorry!! Incorrect Username or Password");
}
}


是用ASPX傳值呼叫,傳遞資料都沒問題(android用TEXTVIEW有顯示回傳的值)
但在if(result.equals("true"))判斷時一直錯誤
發表人: Seachaos
積分: 2432
發表時間: 2011-09-15 23:32:14
您好
可能是你的true並不是true而已
還有其他的html碼在內

以下是我連上去看的結果

[sea:phpCode]
false

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
Sorry!! Incorrect Username or Password

[/sea]
也就是說可能你的true不是只有true,而是後面還有一堆的html碼
(可能你用的ASP是MVC的架構,要設定一下ASP,把這個功能關閉)
發表人: 訪客
發表時間: 2011-09-16 01:06:06
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim userid As String
Dim userpw As String
userid = Request.Form.Item("username") '接收手機端應用程式傳來的帳號密碼
userpw = Request.Form.Item("password")
If userid = "abc" And userpw = "123" Then
Response.Write("SUCCESS")
Else
Response.Write("ERROR")
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
</html>

這是我網頁端的程式碼
我記得我應該是用asp.net WEB
那還有MVC的問題嗎?
而且我看android端import 都是 org.apache
這有差別嗎?
(第一次寫ANDROID還煩請教)
發表人: Seachaos
積分: 2432
發表時間: 2011-09-17 10:25:53
[quote]訪客 提到:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind=...[/quote]
那你可能要把所以的Html Tag拿掉
剩下判斷部份就是
可能如下
另外這Code寫的是success和error, 不是true or false,可能你要改一下

[sea:javaCode]
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb"%>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim userid As String
Dim userpw As String
userid = Request.Form.Item("username") '接收手機端應用程式傳來的帳號密碼
userpw = Request.Form.Item("password")
If userid = "abc" And userpw = "123" Then
Response.Write("SUCCESS")
Else
Response.Write("ERROR")
End If
End Sub
</script>
[/sea]
發表人: nobb
積分: 58
發表時間: 2011-09-20 22:06:32
已經解決了,謝謝!
發表人: Seachaos
積分: 2432
發表時間: 2011-09-21 00:24:43
不會,有Android程式設計上的問題歡迎一起研究 :)