直接上代码
LoginActivity.java
package LoginActivity.activity;
import LoginActivity.bean.StringUtils;
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.EditText;
import android.widget.Toast;
import com.example.haierloginactivity.R;
public class haierLoginActivity extends Activity {
private Button btjButton;
private EditText uname;
private EditText password;
private InputMethodManager in;
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
in = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
btjButton = (Button)findViewById(R.id.button);
uname = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
btjButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
in.hideSoftInputFromWindow(v.getWindowToken(), 0);
String name = uname.getText().toString();
String pwd = password.getText().toString();
if(StringUtils.isEmpty(name)){
Toast.makeText(v.getContext(), R.string.login_uname_null, Toast.LENGTH_SHORT);
return;
}
if(StringUtils.isEmpty(pwd)){
Toast.makeText(v.getContext(), R.string.login_pwd_null, Toast.LENGTH_SHORT);
return;
}
login(name, pwd);
}
});
}
private void login(final String name, final String pwd){
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if(msg.what == 1){
//提示登陆成功
Toast.makeText(haierLoginActivity.this, R.string.msg_login_success, 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 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();
}
}
post.java
package LoginActivity.activity;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.HttpClient;
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 = new DefaultHttpClient();
String strurl = "//此处改成自己的url";
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());
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);//发送数据
HttpResponse httpResponse = httpClient.execute(request);//获得相应
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;
}
}
要记得给设备访问网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>