欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android完整的前后端交互參考案例

 更新時(shí)間:2021年09月10日 10:46:55   作者:Time .  
這篇文章主要介紹了Android完整的前后端交互參考案例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Android 前端獲取數(shù)據(jù)并顯示

API 連接獲取(~~Api.java)

文件創(chuàng)建

新建Kotlin class/file 中的 Interface 文件。

文件內(nèi)容

public interface CleaningDisinfectionApi {
    //綜合查詢清洗消毒列表
    @POST("formatdisinfection/getPage")
    Observable<CleaningDisinfectionData.CleaningDisinfectionInfoList> getCleaningDisinfection(@Header("Range") String page, @Body CleaningDisinfectionData.SearchCleaningDisinfectionRequest searchCleaningDisinfectionRequest);
}

POST

內(nèi)容格式固定,內(nèi)容根據(jù)結(jié)構(gòu)文檔請(qǐng)求不同數(shù)據(jù)組

@POST("formatdisinfection/getPage")

其格式固定,POST后內(nèi)容代表根據(jù)接口文檔進(jìn)行填寫(xiě)。

在此接口中,請(qǐng)求數(shù)據(jù)組如圖所示==

Observable

Observable<CleaningDisinfectionData.CleaningDisinfectionInfoList> getCleaningDisinfection(@Header("Range") String page, @Body CleaningDisinfectionData.SearchCleaningDisinfectionRequest searchCleaningDisinfectionRequest);
CleaningDisinfectionData.CleaningDisinfectionInfoList

返回?cái)?shù)據(jù)列表,數(shù)據(jù)內(nèi)容根據(jù)需求自行設(shè)計(jì)

getCleaningDisinfection(@Header("Range") String page, @Body CleaningDisinfectionData.SearchCleaningDisinfectionRequest searchCleaningDisinfectionRequest)

頭部

函數(shù)格式確定,
Header為指定的內(nèi)容,
Body是數(shù)據(jù)請(qǐng)求文件(~~~~~~Data.~~~~~~Request)對(duì)象,在此請(qǐng)求數(shù)據(jù)中,一般請(qǐng)求整個(gè)完整的對(duì)象數(shù)據(jù)組。
設(shè)置數(shù)據(jù)是根據(jù)不同的需求顯示不同的數(shù)據(jù)。

數(shù)據(jù)請(qǐng)求以及設(shè)定(~~Data.java)

此部分主要完成利用Api接口,完成完整數(shù)據(jù)組的獲取,以及根據(jù)不同需求設(shè)定不同的返回?cái)?shù)據(jù)列表。

數(shù)據(jù)請(qǐng)求函數(shù)(public static class ~~~Request)

此函數(shù)請(qǐng)求完整的數(shù)據(jù)組。

即*根據(jù)接口文檔參數(shù)內(nèi)容,定義所有的成員變量,并于文檔參數(shù)一一對(duì)應(yīng)*,并定義其get和set方法。

private String date;//消毒日期
        private String amount;//餐具數(shù)量
        private  String coaCode;//企業(yè)許可證號(hào)
        private String coaName;//許可證企業(yè)名稱
        private String entCreditCode;//企業(yè)社會(huì)信用代碼
        private String enterpriseName;//企業(yè)名稱
        private String name;//餐具名稱
        private String way;//消毒方式
        private String person;//操作人姓名
        private String remark;//備注
        private String start;//開(kāi)始日期
        private String end;//結(jié)束日期
        private String addTime;
        private String operator;
        private String operatorIp;
        private String operatorTime;


        private int id;
        private int start1;//開(kāi)始時(shí)間 小時(shí)
        private int start2;//開(kāi)始時(shí)間 分
        private int end1;//結(jié)束時(shí)間 小時(shí)
        private int end2;//結(jié)束時(shí)間  秒
        private int area;//企業(yè)所在區(qū)域
        private int caId;//從業(yè)人員ID

請(qǐng)求參數(shù)

數(shù)據(jù)返回列表函數(shù)( public static class ~~~List)

public static class CleaningDisinfectionInfoList{
        private String MESSAGE;
        private String STATUS;
        private List<Data> data;

        public String getMESSAGE() {
            return MESSAGE;
        }

        public void setMESSAGE(String MESSAGE) {
            this.MESSAGE = MESSAGE;
        }

        public String getSTATUS() {
            return STATUS;
        }

        public void setSTATUS(String STATUS) {
            this.STATUS = STATUS;
        }

        public List<Data> getData() {
            return data;
        }

        public void setData(List<Data> data) {
            this.data = data;
        }

        public static class Data implements Serializable{
            private String date;//消毒日期
            private String enterpriseName;//企業(yè)名稱
            private String amount;//餐具數(shù)量
            private String name;//餐具名稱
            private int area;//企業(yè)所在區(qū)域
            
            public String getDate() {
                return date;
            }

            public void setDate(String date) {
                this.date = date;
            }

            public String getEnterpriseName() {
                return enterpriseName;
            }

            public void setEnterpriseName(String enterpriseName) {
                this.enterpriseName = enterpriseName;
            }

            public String getAmount() {
                return amount;
            }

            public void setAmount(String amount) {
                this.amount = amount;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public int getArea() {
                return area;
            }

            public void setArea(int area) {
                this.area = area;
            }
        }
    }
}

成員變量

		private String MESSAGE;/
        private String STATUS;
        private List<Data> data;//變量類(lèi)型為L(zhǎng)ist,即將獲取的數(shù)據(jù)以List返回

返回?cái)?shù)據(jù)列表設(shè)置函數(shù)

public static class Data implements Serializable{
            private String date;//消毒日期
            private String enterpriseName;//企業(yè)名稱
            private String amount;//餐具數(shù)量
            private String name;//餐具名稱
            private int area;//企業(yè)所在區(qū)域
public String getDate() {
                return date;
            }

            public void setDate(String date) {
                this.date = date;
            }
            ············

定義返回?cái)?shù)據(jù)列表中的成員變量,并設(shè)定其getData和setData方法。

數(shù)據(jù)請(qǐng)求以及設(shè)定

數(shù)據(jù)請(qǐng)求

數(shù)據(jù)請(qǐng)求及設(shè)定在activity中完成。

private void getCleanDisinfetionData() {
        cleanningdisinfectionApi.getCleaningDisinfection(pageNo+":10", searchCleaningDisinfectionRequest)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<CleaningDisinfectionData.CleaningDisinfectionInfoList>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {
                                        Log.d(TAG, "onSubscribe:");
                                    }

                                    @Override
                                    public void onNext( CleaningDisinfectionData.CleaningDisinfectionInfoList list) {
                                        Log.d(TAG, "onNext:");
                                        if (list != null&&list.getSTATUS().equals("200") && list.getData() != null) {
                                            cleanDisinfectionList.addAll(list.getData());
                                            setListData();
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable e) { Log.d(TAG, "onError:" + e.getMessage());}

                                    @Override
                                    public void onComplete() {
                                        Log.d(TAG, "onComplete:");
                                    }
                                });
    }
   

數(shù)據(jù)請(qǐng)求操作格式固定,即在activity中調(diào)用~~~Api進(jìn)行請(qǐng)求數(shù)據(jù),并將請(qǐng)求到的數(shù)據(jù)返回到List中。

數(shù)據(jù)顯示

private void setListData(){
        mainScroll.setAdapter(new CleaningDisinfectionAdapter(cleanDisinfectionList));
        if (RetrofitSingleton.dataNumber!=null){
            if (Integer.parseInt(RetrofitSingleton.dataNumber)<10000){
                resultNum.setText(RetrofitSingleton.dataNumber+"(家)");
            }
            else {
                resultNum.setText("10000+(家)");
            }
            if(cleanDisinfectionList.size()==Integer.parseInt(RetrofitSingleton.dataNumber)){
                Toast.makeText(FoodOperationSearchActivity.this,"已加載全部數(shù)據(jù)",Toast.LENGTH_SHORT).show();
                mRefreshLayout.setEnableLoadMore(false);
            }else {
                mRefreshLayout.setEnableLoadMore(true);
            }
        }
    }

數(shù)據(jù)顯示模塊,將請(qǐng)求到的數(shù)據(jù)傳遞到Adapter中,利用自己設(shè)置的Adapter完成數(shù)據(jù)顯示。

Adapter(適配器)設(shè)置

package com.upc.txgc.intelligentmarket2021.Views;

import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.upc.txgc.intelligentmarket2021.HttpRequests.HttpObjects.CleaningDisinfectionData;
import com.upc.txgc.intelligentmarket2021.HttpRequests.HttpObjects.LicenseData;
import com.upc.txgc.intelligentmarket2021.R;
import com.zzhoujay.richtext.spans.LongClickable;
import org.greenrobot.eventbus.EventBus;

import java.util.List;


public class CleaningDisinfectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "CleaningDisinfection";
    private final List<CleaningDisinfectionData.CleaningDisinfectionInfoList.Data> mData;


    public static class ItemClickListener implements View.OnClickListener {

        private CleaningDisinfectionData.CleaningDisinfectionInfoList.Data cleandisinfectionList;
        private int position;

        public void onClick(View v) {
            if (cleandisinfectionList != null) {
                Log.d(TAG, "position:" + position);
                EventBus.getDefault().post(cleandisinfectionList);

                //設(shè)置點(diǎn)擊效果
                //statusChange(v);
            }
        }
    }

    public static class ScrollVH extends RecyclerView.ViewHolder {
        private View mView;
        private TextView enterpriseName;//企業(yè)名稱
        private TextView enterpriseArea;//企業(yè)所在區(qū)域
        private TextView disinfectionDate;//消毒日期
        private TextView name;//物品名稱
        private TextView amount;//物品數(shù)量
        GradientDrawable statusDrawable = new GradientDrawable();
        GradientDrawable licenseStatusDrawable = new GradientDrawable();


//創(chuàng)建自己的view
        public ScrollVH(View v) {
            super(v);
            mView = v;
            enterpriseName = v.findViewById(R.id.enterprise_name);
            enterpriseArea = v.findViewById(R.id.enterprise_area);
            disinfectionDate = v.findViewById(R.id.disinfection_date);
            name = v.findViewById(R.id.name);
            amount = v.findViewById(R.id.amount);
        }
    }
    //數(shù)據(jù)傳遞

    public CleaningDisinfectionAdapter(List<CleaningDisinfectionData.CleaningDisinfectionInfoList.Data>data){
        this.mData = data;
    }



    //綁定適配器界面
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_cleandisinfection, parent,
                        false);
        return new ScrollVH(view);
    }

    //設(shè)置數(shù)據(jù)
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ScrollVH) {

            ScrollVH viewHolder = (ScrollVH) holder;
            if (mData.get(position).getEnterpriseName() != null) {
                viewHolder.enterpriseName.setText(mData.get(position).getEnterpriseName());
            }
            if (mData.get(position).getDate() != null){
                viewHolder.disinfectionDate.setText(mData.get(position).getDate());
            }
            //if (mData.get(position).getArea() != null){
               // viewHolder.enterpriseArea.setText(mData.get(position).getArea());
            //}
            if (mData.get(position).getAmount() != null){
                viewHolder.amount.setText(mData.get(position).getAmount());
            }
            if(mData.get(position).getName()!=null){
                viewHolder.name.setText(mData.get(position).getName());
            }

        }
    }

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

將數(shù)據(jù)通過(guò)構(gòu)造函數(shù)傳遞至適配器之中。

到此這篇關(guān)于Android完整的前后端交互參考案例的文章就介紹到這了,更多相關(guān)Android前后端交互內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論