1. 程式人生 > >android+簡單的登入介面+OKhttp請求

android+簡單的登入介面+OKhttp請求

builder gradle中加:

compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.squareup.okio:okio:1.5.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    testCompile 'junit:junit:4.12'

res佈局:

activity_login

<?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="com.bwie.dierzhoukao.LoginActivity"
    android:weightSum="1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="個人資訊"
        android:gravity="center"
 />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="頭像"
            android:layout_centerVertical="true"
            />
        <ImageView
            android:id="@+id/user_touxiang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@mipmap/ic_launcher"
            />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="使用者名稱"
            android:layout_centerVertical="true"
            />
        <TextView
            android:id="@+id/user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="username欄位"
            />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暱稱"
            android:layout_centerVertical="true"
            />
        <TextView
            android:id="@+id/user_nicheng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="逆流成河~"
            />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="退出登入"
        android:onClick="tuichu"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="61dp"
        android:layout_marginStart="61dp"
        android:layout_marginTop="128dp"
        android:id="@+id/button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳轉到商品搜尋頁面"
            android:onClick="tiaozhuan"
            android:layout_alignBaseline="@+id/button"
            android:layout_alignBottom="@+id/button"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="27dp"
            android:layout_marginEnd="27dp" />
    </RelativeLayout>
</LinearLayout>

activity_main
<?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="com.bwie.dierzhoukao.MainActivity">

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:hint="請輸入手機號" />

    <EditText
        android:id="@+id/et_pass"
        android:hint="請輸入密碼"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <Button
            android:id="@+id/login"
            android:onClick="login"
            android:text="登入"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="74dp"
            android:layout_marginStart="74dp"
            android:layout_marginTop="96dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
        <Button
            android:id="@+id/zhuce"
            android:onClick="zhuce"
            android:text="註冊"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/login"
            android:layout_alignBottom="@+id/login"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="81dp"
            android:layout_marginEnd="81dp" />
    </RelativeLayout>

</LinearLayout>

activity_xiangqing

<?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"
    tools:context="com.bwie.dierzhoukao.XiangqingActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>
activity_zhuce
<?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="com.bwie.dierzhoukao.ZhuceActivity">
    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:hint="請輸入手機號" />

    <EditText
        android:id="@+id/et_pass"
        android:hint="請輸入密碼"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    <Button
        android:id="@+id/zhucechenggong"
        android:onClick="zhucechenggong"
        android:text="註冊"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="98dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="137dp"
        android:layout_marginEnd="137dp" />
    </RelativeLayout>
</LinearLayout>

item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <ImageView
        android:id="@+id/good_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        />
    <TextView
        android:id="@+id/good_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity=""
        android:text="fsdfasdfasd"
        />


</LinearLayout>

MyAdapter

package com.bwie.dierzhoukao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwie.dierzhoukao.R;

import java.util.List;

/**
 * Created by TA on 2017/10/14.
 */

public class MyAdapter extends  RecyclerView.Adapter<MyAdapter.MyViewHolder>{
    private Context context;
    private List<String> namelist ;
    private List<String > imglist ;
    private View view;

    public MyAdapter(Context context, List<String> namelist, List<String> imglist) {
        this.context = context;
        this.namelist = namelist;
        this.imglist = imglist;
    }
    //建立ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.item, parent,
                false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.name.setText(namelist.get(position));
            Glide.with(context).load(imglist.get(position)).into(holder.img);
    }

    @Override
    public int getItemCount() {
        return namelist.size() ;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name;
        ImageView img;

        public MyViewHolder(View itemView) {
            super(itemView);

            name = itemView.findViewById(R.id.good_name);
            img = itemView.findViewById(R.id.good_img);

        }
    }
}
Bean類

匯入util包

LoginActivity

package com.bwie.dierzhoukao;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.nio.charset.MalformedInputException;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

    }
    public void tuichu(View v){
        Intent intent = new Intent(LoginActivity.this , MainActivity.class) ;
        startActivity(intent);
    }
    public void tiaozhuan(View v){
        Intent intent = new Intent(LoginActivity.this , XiangqingActivity.class) ;
        startActivity(intent);
    }
}

MainActivity

package com.bwie.dierzhoukao;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void login(View v){
        Intent intent = new Intent(MainActivity.this , LoginActivity.class) ;
        startActivity(intent);
    }
    public void zhuce(View v){
        Intent intent = new Intent(MainActivity.this , ZhuceActivity.class) ;
        startActivity(intent);
    }
}

MyApp

package com.bwie.dierzhoukao;

import android.app.Application;
import android.content.Context;

/**
 * Created by TA on 2017/10/14.
 */

public class MyApp extends Application{
    private static Context instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static Context getInstance() {
        return instance;
    }

}

XiangqingActivity

package com.bwie.dierzhoukao;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bwie.dierzhoukao.adapter.MyAdapter;
import com.bwie.dierzhoukao.bean.Bean;
import com.bwie.dierzhoukao.util.GsonArrayCallback;
import com.bwie.dierzhoukao.util.GsonObjectCallback;
import com.bwie.dierzhoukao.util.OkHttp3Utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class XiangqingActivity extends AppCompatActivity {

    String path ="http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage";
    List<String> namelist = new ArrayList<>() ;
    List<String> imglist = new ArrayList<>() ;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiangqing);
        initView();
        getData();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycleview);
    }

    private void getData() {
        OkHttp3Utils.doGet(path, new GsonObjectCallback<Bean>() {
            @Override
            public void onUi(Bean bean) {
                for (int j = 0 ; j < bean.getData().getSubjects().size(); j++){
                    for (int i = 0; i < bean.getData().getSubjects().get(j).getGoodsList().size(); i++) {
                        String name = bean.getData().getSubjects().get(j).getGoodsList().get(i).getGoods_name();
                        String url = bean.getData().getSubjects().get(j).getGoodsList().get(i).getGoods_img();
                        namelist.add(name);
                        imglist.add(url);
                    }
                }



                MyAdapter myAdapter = new MyAdapter(XiangqingActivity.this, namelist , imglist);
                recyclerView.setAdapter(myAdapter);//設定介面卡
                recyclerView.setLayoutManager(new GridLayoutManager(XiangqingActivity.this, 3));
            }

            @Override
            public void onFailed(Call call, IOException e) {

            }
        });
    }
}
ZhuceActivity

package com.bwie.dierzhoukao;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.bwie.dierzhoukao.util.GsonObjectCallback;
import com.bwie.dierzhoukao.util.OkHttp3Utils;

public class ZhuceActivity extends AppCompatActivity {

    String path = "http://120.27.23.105/user/reg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);
        getData();
    }

    private void getData() {
        OkHttp3Utils.doGet(path, new GsonObjectCallback<>() {
        });
    }

    public void zhucechenggong(View v){
        Intent intent = new Intent(ZhuceActivity.this , MainActivity.class) ;
        startActivity(intent);
    }
}
清單檔案

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bwie.dierzhoukao">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <application

        android:name=".MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LoginActivity" />
        <activity android:name=".ZhuceActivity" />
        <activity android:name=".XiangqingActivity"></activity>
    </application>

</manifest>