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>




原创粉丝点击