package com.example.logintest;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.logintest.model.LoginResult;
import com.example.logintest.utils.HttpUtils;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private Handler handler;
private EditText username;
private EditText password;
private Button btn_login;
private Button btn_register;
private TextView txtResult;
private String url = "http://192.168.0.101/myWorks/login.php";
private String url1 = "http://192.168.0.1/Test/json_array.php";
LoginResult m_result;
Map<String, String> map = new HashMap<String, String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//处理登录成功消息
handler = new Handler(){
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case 123:
try
{
//获取用户登录的结果
LoginResult result = (LoginResult)msg.obj;
String userName = result.getNicename();
txtResult.setText(userName+" 成功登录!");
Toast.makeText(LoginActivity.this, "您成功登录",Toast.LENGTH_SHORT).show();
//跳转到登录成功的界面
Intent intent = new Intent(LoginActivity.this, LoginActivity1.class);
startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
break;
default:
break;
}
}
};
Bundle bundle = this.getIntent().getExtras();
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
btn_login = (Button)findViewById(R.id.btn_login);
btn_register = (Button)findViewById(R.id.btn_register);
txtResult = (TextView)findViewById(R.id.txtResult);
btn_login.setOnClickListener(this);
btn_register.setOnClickListener(this);
if (bundle != null)
{
username.setText(bundle.getString("empNo"));
password.setText(bundle.getString("pass"));
}
}
private LoginResult parseJSONWithGson(String jsonData)
{
Gson gson = new Gson();
return gson.fromJson(jsonData, LoginResult.class);
}
public void onClick(View v)
{
int id = v.getId();
switch (id)
{
case R.id.btn_login:
new Thread(new Runnable() {
@Override
public void run() {
try
{
//POST信息中加入用户名和密码
map.put("uid", username.getText().toString().trim());
map.put("pwd", password.getText().toString().trim());
//HttpUtils.httpPostMethod(url, json, handler);
HttpUtils.post(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("DaiDai", "OnFaile:",e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseBody = response.body().string();
m_result = parseJSONWithGson(responseBody);
//发送登录成功的消息
Message msg = handler.obtainMessage();
msg.what = 123;
msg.obj = m_result; //把登录结果也发送过去
handler.sendMessage(msg);
}
}, map);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
break;
case R.id.btn_register:
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
自己实现的Android PHP JSON实现登陆注册功能.rar (78个子文件)
自己实现的Android PHP JSON实现登陆注册功能
客户端
MyPhpLogin
gradlew 5KB
settings.gradle 16B
.idea
libraries
support_core_ui_25_3_1.xml 788B
junit_4_12.xml 477B
appcompat_v7_25_3_1.xml 770B
javax_inject_1.xml 324B
javax_annotation_api_1_2.xml 358B
exposed_instrumentation_api_publish_0_5.xml 709B
support_annotations_25_3_1.xml 513B
support_fragment_25_3_1.xml 794B
hamcrest_library_1_3.xml 342B
espresso_idling_resource_2_2_2.xml 691B
support_core_utils_25_3_1.xml 806B
support_vector_drawable_25_3_1.xml 649B
support_compat_25_3_1.xml 782B
hamcrest_core_1_3.xml 526B
jsr305_2_0_1.xml 330B
animated_vector_drawable_25_3_1.xml 654B
hamcrest_integration_1_3.xml 354B
okhttp_3_2_0.xml 517B
support_media_compat_25_3_1.xml 818B
espresso_core_2_2_2.xml 636B
okio_1_6_0.xml 501B
rules_0_5.xml 730B
javawriter_2_1_1.xml 330B
gson_2_7.xml 497B
support_v4_25_3_1.xml 420B
runner_0_5.xml 736B
runConfigurations.xml 564B
misc.xml 2KB
compiler.xml 686B
workspace.xml 176KB
gradle.xml 722B
modules.xml 357B
copyright
profiles_settings.xml 76B
build
intermediates
dex-cache
cache.xml 8KB
generated
mockable-android-25.jar 32.3MB
gradle
wrapper
gradle-wrapper.properties 233B
gradle-wrapper.jar 52KB
local.properties 428B
gradlew.bat 2KB
MyPhpLogin.iml 864B
gradle.properties 878B
.gitignore 127B
app
src
androidTest
java
com
example
logintest
ExampleInstrumentedTest.java 772B
test
java
com
example
logintest
ExampleUnitTest.java 415B
main
AndroidManifest.xml 884B
res
mipmap-hdpi
ic_launcher.png 3KB
mipmap-xxxhdpi
ic_launcher.png 10KB
mipmap-xhdpi
ic_launcher.png 5KB
menu
login.xml 243B
main.xml 318B
register.xml 322B
mipmap-mdpi
ic_launcher.png 2KB
drawable
ic_launcher.png 7KB
search.png 1KB
layout
activity_login1.xml 775B
activity_login.xml 3KB
activity_register.xml 7KB
values-w820dp
dimens.xml 364B
values
colors.xml 214B
strings.xml 76B
styles.xml 394B
dimens.xml 269B
mipmap-xxhdpi
ic_launcher.png 8KB
java
com
example
logintest
utils
HttpUtils.java 2KB
model
LoginResult.java 825B
LoginActivity1.java 351B
LoginActivity.java 6KB
RegisterActivity.java 5KB
libs
app.iml 13KB
.gitignore 8B
build.gradle 992B
proguard-rules.pro 662B
build.gradle 521B
服务端
myWorks
conn.php 380B
login.php 1KB
register.php 857B
共 78 条
- 1
资源评论
- 某潜水用户2018-08-06有点小问题
- qihuiji89822017-08-05有文档解释就好了
- 湛江闲人2019-06-10不错 可以编译通过
天马流星2719
- 粉丝: 55
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功