Android后台开发4

Android后台开发4,第1张

概述packagecom.imau.studentcms;importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;importandroid.app.Dialog;importandroid.os.Bundle;importandroid.os.Handler;importandroid.os.Message;importandroid.view.View;import

package com.imau.studentCMS;

import androIDx.annotation.NonNull;
import androIDx.appcompat.app.AppCompatActivity;

import androID.app.Dialog;
import androID.os.Bundle;
import androID.os.Handler;
import androID.os.Message;
import androID.vIEw.VIEw;
import androID.Widget.AbsListVIEw;
import androID.Widget.ImageVIEw;
import androID.Widget.ListVIEw;
import androID.Widget.Toast;

import com.alibaba.fastJson.JsON;
import com.alibaba.fastJson.JsONArray;
import com.alibaba.fastJson.JsONObject;
import com.imau.Adapter.StudentAdapter;
import com.imau.Dialog.MyDialog;
import com.imau.entitry.Student;
import com.imau.util.MyHandler;
import com.imau.util.MyhttpUtil;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

private ListVIEw ListVIEw;private StudentAdapter adapter;private List<Student> stus=new ArrayList<>();private ImageVIEw add;private int pageNo=0;

// private MyHandler handler2=new MyHandler();

private Handler handler=new Handler(){    @OverrIDe    public voID handleMessage(@NonNull Message msg) {        super.handleMessage(msg);        JsONArray data= (JsONArray) msg.obj;        //清空集合        stus.clear();        //遍历数组        for (int i=0;i<data.size();++i){            Student student= data.getobject(i,Student.class);            //存放到stus集合中            stus.add(student);        }        if(msg.what==10){            //加载数据            adapter.addAll(stus);            adapter.notifyDataSetChanged();        }else{            init();        }    }};@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main2);    initStus();}//初始化学生列表信息private voID initStus(){    MyhttpUtil.doGet("http://10.0.2.2:8008/student/page/"+pageNo+"/10",new MyhttpUtil.httpCallbackListener(){        @OverrIDe        public voID onFinsh(String response) {            System.out.println(response);            //解析后台服务Json数据            JsONObject obj = JsON.parSEObject(response);            int code= obj.getInteger("code");            // 判断成功》?            if(code==10000){                //解析Json数组                JsONArray data = obj.getJsONArray("data");                Message message=new Message();                message.obj=data;                handler.sendMessage(message);            }        }    });}private voID init(){    add=findVIEwByID(R.ID.add);    add.setonClickListener(new MyListener());    ListVIEw=findVIEwByID(R.ID.lv);    adapter=new StudentAdapter(Main2Activity.this,R.layout.stu_layout,stus);    ListVIEw.setAdapter(adapter);    //分页动态加载事件   ListVIEw.setonScrollListener(new MyListener());}class MyListener implements VIEw.OnClickListener, AbsListVIEw.OnScrollListener {    @OverrIDe    public voID onClick(VIEw v) {        MyDialog dialog=null;        //1.创建对话框        MyDialog.Builder builder=new MyDialog.Builder(Main2Activity.this ,null);        builder.setPositivebutton("确定", new MyDialog.OnClickListener() {            @OverrIDe            public voID onClick(Dialog dialog,Student student) {                MyhttpUtil.doPost("http://10.0.2.2:8008/student/save" , student,new MyhttpUtil.httpCallbackListener() {                    @OverrIDe                    public voID onFinsh(String response) {                        System.out.println(response);                        //解析后台服务Json数据                        JsONObject obj = JsON.parSEObject(response);                        int code= obj.getInteger("code");                        // 判断成功》?                        if(code==10000){                        }                    }                });                Toast.makeText(Main2Activity.this,"添加成功!",Toast.LENGTH_LONG).show();                dialog.cancel();            }        });        dialog=builder.create();        dialog.show();    }    @OverrIDe    public voID onScrollStateChanged(AbsListVIEw vIEw, int scrollState) {        if(scrollState==0){            pageNo++;            MyhttpUtil.doGet("http://10.0.2.2:8008/student/page/"+pageNo+"/10",new MyhttpUtil.httpCallbackListener(){                @OverrIDe                public voID onFinsh(String response) {                    //解析后台服务Json数据                    JsONObject obj = JsON.parSEObject(response);                    int code= obj.getInteger("code");                    // 判断成功》?                    if(code==10000){                        //解析Json数组                        JsONArray data = obj.getJsONArray("data");                        Message message=new Message();                        message.what=10;                        message.obj=data;                        handler.sendMessage(message);                    }                }            });        }    }    @OverrIDe    public voID onScroll(AbsListVIEw vIEw, int firstVisibleItem, int visibleItemCount, int totalitemCount) {    }}

}

package com.imau.studentCMS;

import androIDx.annotation.NonNull;
import androIDx.appcompat.app.AppCompatActivity;

import androID.content.Intent;
import androID.os.Bundle;
import androID.os.Handler;
import androID.os.Message;
import androID.vIEw.VIEw;
import androID.Widget.EditText;
import androID.Widget.Toast;

import com.alibaba.fastJson.JsON;
import com.alibaba.fastJson.JsONObject;
import com.imau.util.MyHandler;
import com.imau.util.MyhttpUtil;
import com.imau.util.MyRequstUtil;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

private EditText uname;private EditText upass;public static Object data;private Handler handler=new Handler(){    @OverrIDe    public voID handleMessage(@NonNull Message msg) {        super.handleMessage(msg);        if((Integer) msg.obj==10000){            Intent intent=new Intent(MainActivity.this,Main2Activity.class);            startActivity(intent);        }else{            Toast.makeText(MainActivity.this,"用户名密码错误,重新输入",Toast.LENGTH_LONG).show();        }    }};@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    uname=findVIEwByID(R.ID.name);    upass=findVIEwByID(R.ID.pass);}//登录按钮事件方法public voID login(VIEw vIEw){    //1.获取输入用户名密码    String name=uname.getText().toString();    String pass=upass.getText().toString();    //2.验证输入....    if("".equals(name)||name==null){        Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_LONG).show();        uname.setFocusable(true);    }else if("".equals(pass)||pass==null){        Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_LONG).show();        upass.setFocusable(true);    }else{        //3.请求后台:用户名 密码查询用户名        HashMap<String,Object> params=new HashMap<>();        params.put("uname",name);        params.put("upass",pass);        //访问地址:本地的电脑:localhost 10.0.2.2        MyRequstUtil.loginController(params);        //获取值        System.out.println("data==="+MyHandler.data);

// MyhttpUtil.doPost(“http://10.0.2.2:8008/user/login”, params,new MyhttpUtil.httpCallbackListener() {
//
// @OverrIDe
// public voID onFinsh(String response) {
//
// System.out.println(response);
//
// //4.解析后台数据:
// //普通Json串解析;
// //定义一个服务后台一致的实体类;对象解析
// JsONObject obj =JsON.parSEObject(response);
//
// int code= obj.getInteger(“code”);
// //发送消息
// Message message=new Message();
// //判断
// if(code==10000){
//
// message.what=1;
// message.obj=10000;
//
// }else{
//
// message.what=1;
// message.obj=10001;
// }
//
// handler.sendMessage(message);
//
// }
// });

    }}

}

package com.imau.util;

import androID.os.Handler;
import androID.os.Message;

import androIDx.annotation.NonNull;

import com.imau.studentCMS.MainActivity;

public class MyHandler extends Handler {

public static Object data;private static MyHandler handler;@OverrIDepublic voID handleMessage(@NonNull Message msg) {        super.handleMessage(msg);        if(msg.what==RequestCode.LOGINI){            data=msg.obj;        }}private MyHandler(){}public static  MyHandler getInstance(){    if(handler==null){        handler=new MyHandler();    }    return  handler;}

}

总结

以上是内存溢出为你收集整理的Android后台开发4全部内容,希望文章能够帮你解决Android后台开发4所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046665.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存