Androidログイン機能実装

4873 ワード

APPに必要な機能登録の実装コード:
効果図:
              Android登录功能实现_第1张图片
要求が成功した後に返されるデータ:
{
    "description": "    ",
    "flag": "success",
    "id": "457"
}

1、ログインレイアウトactivity_main:


    
        
        
        

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;
            }
        }
    };
}
ネットワーク権限の追加も忘れないでください.