Android智慧城市代码——登录
生活随笔
收集整理的這篇文章主要介紹了
Android智慧城市代码——登录
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
登錄:
package com.example.myapplication.view;import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.viewpager.widget.ViewPager;import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;import com.example.myapplication.MainActivity; import com.example.myapplication.R; import com.example.myapplication.basedata.Connent; import com.example.myapplication.fragment.HomeFragment; import com.google.gson.Gson;import org.json.JSONException; import org.json.JSONObject;import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import okhttp3.Call; import okhttp3.Callback; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response;public class LoginActivity extends AppCompatActivity {private EditText edit_username,edit_password;private Button btn_login;private TextView text_register;private String username,password;OkHttpClient okHttpClient;Handler handler; // private String ipAddress = "http://124.93.196.45:10001"; // private String loginAddress = "/prod-api/api/login";private String token;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);okHttpClient = new OkHttpClient.Builder().build();handler = new Handler(Looper.getMainLooper());bindID();getData();}private void bindID() {edit_username = (EditText) findViewById(R.id.login_username);edit_password = (EditText) findViewById(R.id.login_password);btn_login = (Button) findViewById(R.id.btn_login);text_register = (TextView) findViewById(R.id.text_register);}private void getData() {btn_login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {username = edit_username.getText().toString();password = edit_password.getText().toString();if (username.isEmpty()){Toast.makeText(LoginActivity.this,"請(qǐng)輸入用戶名",Toast.LENGTH_LONG).show();return;}else if (password.isEmpty()){Toast.makeText(LoginActivity.this,"請(qǐng)輸入密碼",Toast.LENGTH_LONG).show();return;}else { // String url =ipAddress+loginAddress;Map data = new HashMap();data.put("username",username);data.put("password",password);String json = new Gson().toJson(data);RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), json);Request request = new Request.Builder().url(Connent.ipAddress+Connent.loginAddress)//http://124.93.196.45:10001/prod-api/api/login.post(requestBody).build();Call call = okHttpClient.newCall(request);call.enqueue(new Callback() {@Overridepublic void onFailure(@NonNull Call call, @NonNull IOException e) {}@Overridepublic void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {if (response!=null&&response.isSuccessful()){String data = response.body().string();Log.i("我的數(shù)據(jù)" ,data);handler.post(new Runnable() {@Overridepublic void run() {JSONObject jsonObject = null;try {jsonObject=new JSONObject(data);} catch (JSONException e) {e.printStackTrace();}if (jsonObject.optString("code").equals("200")){SharedPreferences sp = getSharedPreferences("MyData",MODE_PRIVATE);SharedPreferences.Editor editor = sp.edit();token = jsonObject.optString("token");editor.putString("token",token);editor.putString("username",username);editor.commit();startActivity(new Intent(LoginActivity.this, MainActivity.class));Toast.makeText(LoginActivity.this,jsonObject.optString("msg"),Toast.LENGTH_LONG).show();}else {Toast.makeText(LoginActivity.this,jsonObject.optString("msg"),Toast.LENGTH_LONG).show();}}});}}});}}});text_register.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(LoginActivity.this,RegisterActivity.class));LoginActivity.this.finish();}});}}布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".view.LoginActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="登錄"android:textSize="30sp"android:gravity="center"android:padding="10dp"android:textColor="@color/white"android:background="@color/blue"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="100dp"android:padding="20dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用戶名:"android:textSize="25sp"/><EditTextandroid:id="@+id/login_username"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/edit_background"android:textSize="25sp"android:textColor="@color/black"android:inputType="text"android:textColorHint="#AAAAAA"android:hint="@string/edit_username"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="20dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 碼:"android:textSize="25sp"/><EditTextandroid:id="@+id/login_password"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/edit_background"android:textSize="25sp"android:textColor="@color/black"android:inputType="textPassword"android:textColorHint="#AAAAAA"android:hint="@string/edit_password"/></LinearLayout><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="60dp"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:text="@string/login"android:textSize="25sp"app:backgroundTint="@null"android:background="@drawable/login_btn_bg" /><TextViewandroid:id="@+id/text_register"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="200dp"android:gravity="center"android:textColor="@color/blue"android:text="暫無帳號(hào),立即注冊(cè)" /></LinearLayout>總結(jié)
以上是生活随笔為你收集整理的Android智慧城市代码——登录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 免费的天气API相关编码(中国城市代码检
- 下一篇: Android和iPhone浏览器大战,