package com.itcast.test03;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_userPsd;
private Button login;
private Button signUp;
private CheckBox save;
private String user,pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText)findViewById(R.id.et_number);
et_userPsd = (EditText)findViewById(R.id.et_password);
login=(Button)findViewById(R.id.login);
signUp=(Button)findViewById(R.id.signUp);
save = (CheckBox)findViewById(R.id.save);
//登录按钮的点击事件
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//将输入的用户名和密码转换成字符串
String name =et_username.getText().toString();
String pwd = et_userPsd.getText().toString();
//调用login方法
login(name, pwd);
}
});
}
private void login(final String name, final String pwd){
//创建Handler对象
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if(msg.what == 1){
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//提示登陆成功
finish();
}else if(msg.what == 0){
Log.i(name, msg.toString());
}else if(msg.what == -1){
Log.i("==============",msg.toString());
}
}
};
new Thread(){
public void run() {
//创建Message对象
Message msg =new Message();
try {
Post po = new Post();
String infoo = po.logina(name, pwd);
if(infoo != null){
msg.what = 1;//成功
msg.obj = infoo;
}else{
msg.what = 0;//失败
msg.obj = "2";
}
} catch (Exception e) {
e.printStackTrace();
msg.what = -1;
msg.obj = e;
}
handler.sendMessage(msg);
}
}.start();
}
}
package com.itcast.test03;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class Post {
public String logina(String name, String pwd)throws ParseException, IOException, JSONException{
//获取到HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
String strurl = "http://10.6.78.213:2016/xampp/sse/index.php/home/Index/server_info";
HttpPost request = new HttpPost(strurl);
request.addHeader("Accept","application/json");
request.addHeader("Content-Type","application/json");//还可以自定义增加header
JSONObject param = new JSONObject();//定义json对象
param.put("sequenceId", "87620056570355357690");
param.put("accType", "0");
param.put("loginId", name);
param.put("password", pwd);
//param.put("thirdpartyAppId", "");
//param.put("thirdpartyAccessToken", "");
param.put("loginType", "1");
Log.i("===========", param.toString());
System.out.println("1+===========");
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);//发送数据
HttpResponse httpResponse = httpClient.execute(request);//获得相应
System.out.println("2+===========");//进行一行一行的调试时无法打印此语句。原因就是无法成功连接到 网络
int code = httpResponse.getStatusLine().getStatusCode();
System.out.print(code);
String result = EntityUtils.toString(httpResponse.getEntity());
JSONObject result1 = new JSONObject(result);
String info = (String) result1.get("retInfo");
Log.i("=============", info);
return info;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有