Android登录功能实现
来源:互联网 发布:crossover软件怎么样 编辑:程序博客网 时间:2024/05/29 12:24
APP中必备功能登录的实现代码:
效果图:
请求成功后返回的数据:
{ "description": "登录成功", "flag": "success", "id": "457"}
1、登录布局activity_main:
<?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" tools:context="com.wdl.login.MainActivity"> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <EditText android:id="@+id/photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号"/> <EditText android:id="@+id/pwd" android:password="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码"/> <Button android:id="@+id/btn_log" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录"/> </LinearLayout></RelativeLayout>
2、MainActivity.java部分代码:
代码中需要使用到okhttp架包,这里我就不展示怎么添加了(博文如何添加架包:http://blog.csdn.net/qq_26650589/article/details/73159658)
public class MainActivity extends AppCompatActivity { //用户名,密码 private EditText photo, pwd; private Button btn; public String result,is; private String photostring,pwdstring; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); photo = (EditText) findViewById(R.id.photo); pwd = (EditText) findViewById(R.id.pwd); btn = (Button) findViewById(R.id.btn_log); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取文本框中的内容 photostring = photo.getText().toString().trim(); pwdstring = pwd.getText().toString().trim(); Okhttp(photostring, pwdstring); } }); } public void Okhttp(final String pthot, final String pwd) { new Thread(new Runnable() {//开启线程 @Override public void run() { FormBody body =new FormBody.Builder() .add("phone",pthot) //提交参数电话和密码 .add("pwd",pwd) .build(); Request request = new Request.Builder() .url("http://192.168.1.6/Login/login") //请求的地址 .post(body) .build(); OkHttpClient client=new OkHttpClient(); try { Response response = client.newCall(request).execute(); result = response.body().string(); //获得值 JX(result); //解析 } catch (IOException e) { e.printStackTrace(); } } }).start(); } private void JX(String date){ try { JSONObject jsonObject=new JSONObject(date); String flag = jsonObject.getString("flag");//获取返回值flag的内容 if (flag.equals("success")){ is = jsonObject.getString("description"); }else{ is = jsonObject.getString("description"); } Message message = new Message(); message.what = 1; handler.sendMessage(message); } catch (JSONException e) { e.printStackTrace(); } } private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: Toast.makeText(MainActivity.this, is,Toast.LENGTH_LONG).show(); break; } } };}还有不要忘记添加网络权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
阅读全文
0 0
- Android登录功能实现
- Android 实现登录注册功能
- android 登录界面功能实现
- Android 实现登录注册功能
- Android 实现登录界面和功能实例
- Android PHP JSON 登录注册功能实现
- Android实现登录界面和功能实例
- Android实现登录界面和功能实例
- android 自动登录功能的实现
- Android 实现登录界面和功能实例
- Android:登录实现记住密码功能
- Android实现登录界面和功能实例
- Android实现登录界面和功能实例
- Android实现登录界面功能和实现详解
- 实现登录过滤器功能
- session实现登录功能
- EJB实现登录功能
- Qt 实现登录功能
- 输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数
- java Class getDeclaredFields() 与getFields()的区别
- android:windowSoftInputMode
- 【树形DP】UVa 12186 Another Crisis
- 关于JDK1.8和eclipse版本不兼容导致的Arrays cannot be resolved
- Android登录功能实现
- ACM数论基础训练题解之最大质因子
- Kotlin中常用操作符和用法介绍
- 8:渐变(背景-属性)-1.1.4径向渐变
- SSM框架搭建项目整合学习笔记
- Spring MVC的跨域配置
- React学习(四)组件
- JavaScript中的DOM操作
- iOS开发:字符串设置指定内容的文字颜色、文字大小、文字字体类型