1、创建服务器
1) 登陆的action
public class LoginAction extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//处理客户的post请求
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("admin")&&password.equals("12345")) {
ResultEntity entity = new ResultEntity();
entity.setResultCode(1);
entity.setResultMsg("success");
HashMap<String,ResultEntity> map = new HashMap<String,ResultEntity>();
map.put("result", entity);
String json_value = JSONSerializer.toJSON(map).toString();
writer.print(json_value);
}
writer.flush();
writer.close();
}
}
2)实体类
public class ResultEntity {
private int resultCode;
private String resultMsg;
public int getResultCode() {
return resultCode;
}
public void setResultCode(int resultCode) {
this.resultCode = resultCode;
}
public String getResultMsg() {
return resultMsg;
}
public void setResultMsg(String resultMsg) {
this.resultMsg = resultMsg;
}
}
2、客户端:
1)创建登陆工具类
/**
* 定义了login操作,使用RxAndroid的编程思想
* @param url
* @param params
* @return
*/
public static Observable<String> login(String url, Map<String ,String> params){
return Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
if (!subscriber.isUnsubscribed()){
FormBody.Builder builder = new FormBody.Builder();
if (params != null && !params.isEmpty()){
for (Map.Entry<String, String> entry: params.entrySet()){
builder.add(entry.getKey(), entry.getValue());
}
}
RequestBody requestBody = builder.build();
//构建post请求
Request request = new Request.Builder().url(url).post(requestBody).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
subscriber.onError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
//订阅消息
subscriber.onNext(response.body().toString());
}
subscriber.onCompleted();//访问结束
}
});
}
}
});
}
}
2)创建解析json工具类
public class JsonUtils {
public static boolean parserJson(String jsonStr){
boolean flag = false;
try {
JSONObject object = new JSONObject(jsonStr).getJSONObject("result");
int result = object.getInt("resultCode");
flag = result != 1 ? false : true;
} catch (JSONException e) {
e.printStackTrace();
}
return flag;
}
}
3、创建登陆
public class LoginActivity extends ActionBarActivity {
private final String TAG = MainActivity.class.getSimpleName();
private Button button;
private EditText username;
private EditText password;
private ProgressDialog dialog;
private final String LOGIN_URL = "http://192.168.31.239:8080/webproject/LoginAction";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
button = (Button) this.findViewById(R.id.button);
username = (EditText) this.findViewById(R.id.editText);
dialog = new ProgressDialog(this);
dialog.setTitle("login......");
password = (EditText) this.findViewById(R.id.editText2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, String> params = new HashMap<String, String>();
params.put("username", username.getText().toString().trim());
params.put("password", password.getText().toString().trim());
LoginUtil.login(LOGIN_URL, params).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
dialog.dismiss();
}
@Override
public void onError(Throwable e) {
Log.i(TAG, e.getMessage());
}
@Override
public void onNext(String s) {
dialog.show();
if (JsonUtils.parserJson(s)) {
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
}
}
});
}
});
}
}
4)布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="52dp"
android:text="账号:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_marginTop="39dp"
android:text="密码:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignStart="@+id/editText2"
android:layout_alignTop="@+id/textView"
android:hint="请输入账号" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/textView2"
android:layout_toEndOf="@+id/textView2"
android:ems="10"
android:hint="请输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText2"
android:layout_marginTop="50dp"
android:text="login" />
</RelativeLayout>
—————————————————————————————————————————————————–
JAVA(SSM、SSH等)20项目视频教程,共134G
下载地址:
https://item.taobao.com/item.htm?id=558680925808
———————-课程目录——————————
第01项目:OA办公自动化项目(四套)
第02项目:CRM客户关系管理项目(两套)
第03项目:宅急送项目
第04项目:杰信商贸SSH版
第05项目:电力项目(两套)
第06项目:校内网项目
第07项目:Java邮件开发教程
第08项目:java网上在线支付实战视频
第09项目:俄罗斯方块游戏开发_视频教程
第10项目:交通灯管理系统视频教程
第11项目:银行业务调度系统视频教程
第12项目:供应链系统视频教程
第13项目:网上商城项目
第14项目:药品集中采购系统视频教程
第15项目:杰信商贸SSM版
第16项目:国家税务协同平台项目
第17项目:javaWeb聊天室
第18项目:点餐系统
第19项目:网上书店
第20项目:手机进销存系统
—————————————————————————————————————————————————–