80,350
社区成员
发帖
与我相关
我的任务
分享
class loginButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
src="www.XXXX.asp";
if(userName_ET.getText().toString().trim().equals("")||passWord_ET.getText().toString().trim().equals(""))
{
Toast.makeText(ANS.this,"未填写完整的用户信息,无法登录!", Toast.LENGTH_LONG).show();
}
else
{
CharSequence strDialogTitle=getString(R.string.login_dialog_title);
CharSequence strDialogBody=getString(R.string.login_dialog_body);
myDialog=ProgressDialog.show(ANS.this, strDialogTitle,strDialogBody);
new Thread()
{
public void run()
{
try
{
HttpPost httpRequest = new HttpPost(src);
/*
* NameValuePair实现请求参数的封装
*/
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("MobileNum", userName_ET.getText().toString().trim()));
try
{
/* 添加请求参数到请求对象*/
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/*发送请求并等待响应*/
//设置查询超时
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
//httpResponse.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
//httpResponse.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
/*若状态码为200 ok*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*读返回数据*/
StringBuffer sb = new StringBuffer();
HttpEntity entity = httpResponse.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is,"UTF-8"));
String data = "";
while ((data = br.readLine()) != null) {
sb.append(data);
}
cusListData = sb.toString();
}
else
{
Toast.makeText(ANS.this,"Error Response: "+httpResponse.getStatusLine().toString(), Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(ANS.this,e.getMessage().toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
}
}.start();
}
}
}
/**
* 用Handler方法来更新UI
*/
private Handler handler = new Handler()
{
public void handleMessage(Message message)
{
switch (message.what)
{
case 0:
// 刷新UI,显示数据,并关闭进度条
myDialog.dismiss(); // 关闭进度条
if(!cusListData.equals(""))
{
Intent intent = new Intent();
intent.putExtra("cusList_Str", cusListData);
intent.setClass(ANS.this,MainForm.class);
Toast.makeText(ANS.this,"登录成功!", Toast.LENGTH_LONG).show();
ANS.this.startActivity(intent);
}
else
{
Toast.makeText(ANS.this,"登录超时,请确认当前的网络状态!", Toast.LENGTH_LONG).show();
}
break;
case 1:
myDialog.dismiss();
Toast.makeText(ANS.this,"登录失败,请确认服务器是否开启!", Toast.LENGTH_LONG).show();
break;
}
}
};