android数值比对??从jsp服务端比对客户端内容出错,登入密码

ericrock 发布于 2015/07/23 16:53
阅读 123
收藏 0

有个问题就是我在jsp中,设登入失败回传值比对0,要让我的android客户端这边比对到0就不会执行intent到下一个activity,但是它每次都直接跳过我的判断式,直接执行else,自己抓bug后发现,我request的length长度为8,数值竟然有长度8,然后就不知如何继续解决?有人看得出来问题在那吗?

android

public class LoginActivity extends Activity {

    
EditText htpassword;
EditText htaccount;
Button htlogin;
TextView showmessage;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

htaccount=(EditText)findViewById(R.id.htaccount);
htpassword=(EditText)findViewById(R.id.htpasswod);
htlogin=(Button)findViewById(R.id.htlogin);
showmessage=(TextView)findViewById(R.id.showmessage);

progressDialog = new ProgressDialog(this);

htlogin.setOnClickListener(new OnClickListener() {

@SuppressWarnings("unchecked")
@Override
public void onClick(View v) {
new AT().execute(htaccount.getText().toString(),htpassword.getText().toString());

}
});


}

public class UriAPI {  
        /** 定义一个Uri **/  
    public static final String HTTPCustomer ="http://gn00645224.oicp.net/changtai/serverlet/CustomerServlet";
    //http://gn00645224.oicp.net/changtai/webpage/login/index.jsp

}
    
@SuppressWarnings("rawtypes")
class AT extends AsyncTask{
        
String result="";
    @Override
    protected void onPreExecute() {
    //加载progressDialog
    progressDialog.show();
    progressDialog.setMessage("登入中...");
    progressDialog.setIcon(R.drawable.ic_launcher);
   }

@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub



CharSequence username="";
CharSequence password="";
username=(String)username;
password=(String)password;
 
username= htaccount.getText();
password= htpassword.getText();
 
if(!username.equals("")&&!password.equals("")){
//请求数据
HttpPost httpRequest  = new HttpPost(UriAPI.HTTPCustomer);
//创建参数  
List<NameValuePair> params1=new ArrayList<NameValuePair>(); 
params1.add(new BasicNameValuePair("username", username.toString()));
params1.add(new BasicNameValuePair("password", password.toString()));
//params.add(new BasicNameValuePair("flag","0"));
try {
//对提交数据进行编码
httpRequest.setEntity(new UrlEncodedFormEntity(params1,HTTP.UTF_8));
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//获取响应服务器的数据
if (httpResponse.getStatusLine().getStatusCode()==200) {
//利用字节数组流和包装的绑定数据
byte[] data =new byte[1024];
//先把从服务端来的数据转化成字节数组
data =EntityUtils. toByteArray((HttpEntity)httpResponse.getEntity());  
//再创建字节数组输入流对象   
ByteArrayInputStream bais = new ByteArrayInputStream(data);  
//绑定字节流和数据包装流   
DataInputStream dis = new DataInputStream(bais);  
 //将字节数组中的数据还原成原来的各种数据类型,代码如下:  
result=new String(dis.readUTF());
  Log.i("服务器返回信息:", result);
}
} catch(ClientProtocolException e){  
                   e.printStackTrace();  
               }catch(UnsupportedEncodingException e){  
                   e.printStackTrace();  
               } catch (IOException e) {  
                   e.printStackTrace();  
               }  

}
return result;
}
    
     @Override
   protected void onPostExecute(Object resultObject) {
     
     Log.i("返回信息:", (String)resultObject);
     result= resultObject.toString();
     
     
     if(result.equals(0)){
     Log.i("login retrun","login case 1"+result);
     showmessage.setText(result.toString());
     progressDialog.cancel();
     }
     else{
     Log.i("login retrun","login case 2"+result+","+result.length());
    //获得服务器返回信息成功后
     showmessage.setText(result.toString());
     //取消进度条
     progressDialog.cancel();
     
     Intent intentLogin = new Intent();
     intentLogin.setClass(
 LoginActivity.this,
 MainActivity.class);
 startActivity(intentLogin);
      } 
     
     }
     
     }
   
   } 

jsp

private static final long serialVersionUID = 314719472293387358L;


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//登陆成功标志
String LOGIN_FLAG="";
int LOGIN_FLAG2;
       
//获得客户端提交用户名密码
String username = req.getParameter("username");
String password = req.getParameter("password");
//调用UserDAO中isLogin方法判断数据中用户名密码是否正确
boolean flag=UserDAO.isLogin(username, password);
try {
DataOutputStream output=new DataOutputStream(resp.getOutputStream());
if (flag) {
LOGIN_FLAG="success";
output.writeUTF("服务器端数据:"+LOGIN_FLAG);
   System.out.println(LOGIN_FLAG);
output.writeInt(1);
output.close(); 
}else{
//登录失败  
LOGIN_FLAG2=0;
System.out.println(LOGIN_FLAG2);
output.writeUTF("服务器端数据:"+LOGIN_FLAG2);
 output.close(); 
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

加载中
0
小红书
小红书

服务器端数据:0 

8

返回顶部
顶部