日期:2014-05-16 浏览次数:20486 次
附件有完整代码
package com.example.knowledgedemo; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.example.entity.loginPerson; import com.example.utils.HttpUtil; import com.example.utils.ToastUtil; import com.google.gson.Gson; /** * 知识点:handler的使用、服务端客户端交互、Json、Gson、 * @author somebody * */ public class LoginDemo extends Activity { private EditText loginname, password; private Button login; private ProgressDialog progressdialog; private String name, pwd; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login); loginname = (EditText) findViewById(R.id.loginname); password = (EditText) findViewById(R.id.password); login = (Button) findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login();//点击登录按钮 } }); } private void login() { showProgressDialog();//显示进度框 new Thread() { public void run() { requestServer();//请求服务器 } }.start(); } private void showProgressDialog() { if (progressdialog == null) { progressdialog = new ProgressDialog(this); } if (progressdialog.isShowing()) { progressdialog.dismiss(); } progressdialog.setTitle("提示"); progressdialog.setMessage("正在登陆.."); progressdialog.show(); } private Handler handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); progressdialog.dismiss(); switch (msg.what) { case 0: ToastUtil.longToast(getApplicationContext(), R.string.network_ok);//Toast 提示信息、登录成功 break; case 1: ToastUtil.longToast(getApplicationContext(), R.string.network_not_ok);//登录失败 break; case 2: ToastUtil.longToast(getApplicationContext(), R.string.network_not_ok);//TimeOut 网络连接失败 break; default: break; } } }; private void requestServer(){ name = loginname.getText().toString(); pwd = password.getText().toString();//获取用户名密码 loginPerson person = new loginPerson(); person.setLoginName(name); person.setPassWord(pwd); Gson g = new Gson(); /* * Gson将person对象转换为Json格式的字符串 * 此时obj = {"loginName":"1","passWord":"1"} * */ String obj = g.toJson(person); String result = HttpUtil.httpPostCodeParams("login",obj);//请求服务端、获取返回值 JSONObject resultObject; if(result.startsWith("TimeOut")||result.length()==0){ handler.sendEmptyMessage(2);//请求服务端超时 }else{ try { //如果不超时、返回的数据格式 {"result":"0"} resultObject = new JSONObject(result); result = resultObject.getString("result"); } catch (JSONException e) { e.printStackTrace(); } if(result.equals("0")){ handler.sendEmptyMessage(0);//登录成功 }else if(result.equals("1")){ handler.sendEmptyMessage(1);//登录失败 } } } }
package com.example.entity; public class LoginPerson { private String loginName; private String passWord; public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
package com.example.utils; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURL