HttpPost实现android登录问题请教

橛子干 发布于 2013/08/06 16:41
阅读 1K+
收藏 0

公司要开发一款android产品,我才接触android,分配了一个小功能,负责做登陆功能,可是做了几天都没有成功。求大神指点,谢谢了!

主Activity程序:

public class LoginActivity extends Activity {
	private EditText username = null;
	private EditText password = null;
	private Button btn = null;
	private static final String URL = "http://192.168.1.170:8080/jsp_test/about.jsp";
	private static final int SET = 1 ;
	private static final int ERROR = 0 ;
	private static boolean result = false ;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case SET:
				 Intent intent = new Intent(getApplicationContext(),Manager.class);
				 intent.putExtra("loginInfo", username.getText().toString() +"," + password.getText().toString());
				 LoginActivity.this.startActivity(intent);
				 break ;
			case ERROR:
				Toast.makeText(getApplicationContext(), "用户名或密码错误!",Toast.LENGTH_SHORT).show();
				 break ;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_login);
		username = (EditText) super.findViewById(R.id.UserName);
		password = (EditText) super.findViewById(R.id.PassWord);
		btn = (Button) super.findViewById(R.id.Login);
		btn.setOnClickListener(new OnClickListenerImpl() );
		
	}
		
	public class OnClickListenerImpl implements OnClickListener{
		@Override
		public void onClick(View v) {
			if (Global.isEmpty(password.getText().toString().trim())&& Global.isEmpty(username.getText().toString().trim())) {
					   new Thread(new Runnable() {	
						@Override
						public void run() {
							try {
									HttpPost request = new HttpPost(URL);
									List<NameValuePair> params = new ArrayList<NameValuePair>();
									params.add(new BasicNameValuePair("username",username.getText().toString().trim()));
									params.add(new BasicNameValuePair("password",password.getText().toString().trim()));
									request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
									HttpResponse response = new DefaultHttpClient().execute(request);
									if (response.getStatusLine().getStatusCode() == 200) {
										result =Boolean.parseBoolean(EntityUtils.toString(response.getEntity()).toString()) ;
										Log.v("result", result+"");
										Message msg = new Message() ;
										if(result){
											msg.what = SET ;
											LoginActivity.this.handler.sendMessage(msg);
										}else{
											msg.what = ERROR ;
											LoginActivity.this.handler.sendMessage(msg);
										}
									}	
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							} catch (ClientProtocolException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							}
							
							
						}
					}).start();
				  
			}else{
				  Toast.makeText(getApplicationContext(), "用户名或密码不能为空!",Toast.LENGTH_SHORT).show();
			}
		}	
	} 
	


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}

}

about.jsp代码(测试用,模拟数据库访问):


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
   String username = request.getParameter("username");
   String password = request.getParameter("password");
   if(username.equals("admin") && password.equals("admin")){
 %>
     true
 <%   
   }else{
   %>
     false
   <%    
   }
    %>

LoginActivity中的Global主要是检验输入的用户名或密码是否为空,代码如下:


public class Global {
	public static boolean isEmpty(String str){
			if(str.length()==0){
				return false ;
			}
			    return true ;
	}
}


在手机端传入的参数admin/admin, about.jsp输出的也是true,但是Boolean.parseBoolean(EntityUtils.toString(response.getEntity()).toString()) 获得值始终是false,百思不得其解。


我手机是android4.0系统,网络上说,android4.0系统不允许主线程(UI线程)访问网络,所以代码就写成上面这个样子,第一次发帖,欢迎拍砖。

加载中
返回顶部
顶部