OkHttp请求

public class ShoppingActivity extends BaseActivity implements View.OnClickListener {


    private List<ShoppingBean.DataBean> list = new ArrayList<>();
    private EditText et;
    private TextView change;
    private RecyclerView rv;
    private boolean flag;
    private TextView find;


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


    }


    private void initView() {
        et = (EditText) findViewById(R.id.et);
        change = (TextView) findViewById(R.id.change);
        rv = (RecyclerView) findViewById(R.id.rv);
        change.setOnClickListener(this);
        find = (TextView) findViewById(R.id.find);
        find.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.change:
                if(!flag){
                    show(false);
                    flag=true;
                }else{
                    show(true);
                    flag=false;
                }
                break;
            case R.id.find:
                String s = et.getText().toString().trim();
                httpUtil.doGet("http://120.27.23.105/product/searchProducts?keywords=" + s + "&page=1", null, null, ShoppingBean.class, new OnNetListener() {
                    @Override
                    public void onSuccess(BaseBean baseBean) throws IOException {
                        ShoppingBean shoppingBean = (ShoppingBean) baseBean;
                        list = shoppingBean.getData();
                        show(true);
                    }


                    @Override
                    public void onError(IOException e) {


                    }
                });
                break;
        }
    }


    public void show(boolean flag){
        rv.setLayoutManager(flag ? new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false) : new GridLayoutManager(this,2));
        MyAdapter adapter = new MyAdapter(this,list);
        rv.setAdapter(adapter);
    }
}
public class MyMainActivity extends BaseActivity implements View.OnClickListener {


    private HashMap<String,String> params = new HashMap<>();
    private TextView user;
    private TextView name;
    private Button finish;
    private Button shopping;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_main);
        initView();
        Intent intent = getIntent();
        int uid = intent.getIntExtra("UID",0);


        httpUtil.doGet("http://120.27.23.105/user/getUserInfo?uid="+uid,null,null, MyMainBean.class, new OnNetListener() {
            @Override
            public void onSuccess(BaseBean baseBean) throws IOException {
                MyMainBean myMainBean = (MyMainBean) baseBean;
                String username = myMainBean.getData().getUsername();
                String password = myMainBean.getData().getPassword();
                user.setText(username);
                name.setText(password);
                Toast.makeText(MyMainActivity.this,"请求成功",Toast.LENGTH_SHORT).show();
            }


            @Override
            public void onError(IOException e) {
                Toast.makeText(MyMainActivity.this,"请求失败",Toast.LENGTH_SHORT).show();
            }
        });


    }


    private void initView() {
        user = (TextView) findViewById(R.id.user);
        name = (TextView) findViewById(R.id.name);
        finish = (Button) findViewById(R.id.finish);
        shopping = (Button) findViewById(R.id.shopping);
        finish.setOnClickListener(this);
        shopping.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.finish:
                MyMainActivity.this.finish();
                break;
            case R.id.shopping:
                Intent intent = new Intent(MyMainActivity.this,ShoppingActivity.class);
                startActivity(intent);
                break;
        }public class BaseActivity extends AppCompatActivity {


    private LinkedList<Activity> list = new LinkedList<>();
    protected HttpUtil httpUtil;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list.add(this);
        httpUtil = HttpUtil.getInstance(this);
    }


    public void finishAll(){
        for (Activity ac : list){
            if(!ac.isFinishing()){
                ac.finish();
            }
        }
    }
}
    }
}
public class RegisterActivity extends BaseActivity implements View.OnClickListener {


    private Button register;
    private EditText re_phone;
    private EditText re_pwd;
    private HashMap<String,String> params = new HashMap<>();


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


    private void initView() {


        re_phone = (EditText) findViewById(R.id.re_phone);
        re_pwd = (EditText) findViewById(R.id.re_pwd);
        register = (Button) findViewById(R.id.register);
        register.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register:


                String mobile = re_phone.getText().toString().trim();
                String password = re_pwd.getText().toString().trim();


                params.put("mobile",mobile);
                params.put("password",password);


                httpUtil.doPost(Api.REGISTER, params, RegisterBean.class, new OnNetListener() {
                    @Override
                    public void onSuccess(BaseBean baseBean) throws IOException {


                        Toast.makeText(RegisterActivity.this,"注册成功!",Toast.LENGTH_SHORT).show();
                        RegisterActivity.this.finish();
                    }


                    @Override
                    public void onError(IOException e) {
                        Toast.makeText(RegisterActivity.this,"注册失败!",Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }
}

public class MainActivity extends BaseActivity implements View.OnClickListener {


    private Button bt_zhuce;
    private EditText phone;
    private EditText pwd;
    private Button bt_login;
    private HashMap<String,String> params = new HashMap<>();


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


    private void initView() {


        phone = (EditText) findViewById(R.id.phone);
        pwd = (EditText) findViewById(R.id.pwd);
        bt_zhuce = (Button) findViewById(R.id.bt_zhuce);
        bt_zhuce.setOnClickListener(this);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_zhuce:
                Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_login:


                String mobile = phone.getText().toString().trim();
                String password = pwd.getText().toString().trim();


                params.put("mobile",mobile);
                params.put("password",password);


                httpUtil.doPost(Api.LOGIN, params, LoginBean.class, new OnNetListener() {
                    @Override
                    public void onSuccess(BaseBean baseBean) throws IOException {
                        LoginBean loginBean = (LoginBean) baseBean;
                        int uid = loginBean.getData().getUid();
                        Intent intent = new Intent(MainActivity.this,MyMainActivity.class);
                        intent.putExtra("UID",uid);
                        startActivity(intent);
                        MainActivity.this.finish();
                    }


                    @Override
                    public void onError(IOException e) {
                        Toast.makeText(MainActivity.this,"登录失败!",Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }
}

public class MyAdapter extends RecyclerView.Adapter{


    private Context context;
    private List<ShoppingBean.DataBean> list;
    private static final int TYPE1 = 0;
    private static final int TYPE2 = 1;


    public MyAdapter(Context context, List<ShoppingBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        RecyclerView.ViewHolder holder = null;
        if(viewType == TYPE1){
            View view1 = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
            holder = new Type1ViewHolder(view1);
        }else{
            View view2 = LayoutInflater.from(context).inflate(R.layout.item2, parent, false);
            holder = new Type2ViewHolder(view2);
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof Type1ViewHolder){
            ShoppingBean.DataBean dataBean = list.get(position);
            Type1ViewHolder type1ViewHolder = (Type1ViewHolder) holder;
            type1ViewHolder.item1_price.setText(dataBean.getPrice()+"");
            type1ViewHolder.item1_title.setText(dataBean.getTitle());
            ImageLoader.getInstance().displayImage(dataBean.getImages(),type1ViewHolder.item1_iv);
        }else{
            ShoppingBean.DataBean dataBean = list.get(position);
            Type2ViewHolder type2ViewHolder = (Type2ViewHolder) holder;
            type2ViewHolder.item2_price.setText(dataBean.getPrice()+"");
            type2ViewHolder.item2_title.setText(dataBean.getTitle());
            ImageLoader.getInstance().displayImage(dataBean.getImages(),type2ViewHolder.item2_iv);
        }
    }


    @Override
    public int getItemViewType(int position) {
        return position % 2 == 0? TYPE1 : TYPE2;
    }


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


    private class Type1ViewHolder extends RecyclerView.ViewHolder{


        private final ImageView item1_iv;
        private final TextView item1_title;
        private final TextView item1_price;


        public Type1ViewHolder(View itemView) {
            super(itemView);
            item1_iv = itemView.findViewById(R.id.item1_iv);
            item1_title = itemView.findViewById(R.id.item1_title);
            item1_price = itemView.findViewById(R.id.item1_price);
        }
    }


    private class Type2ViewHolder extends RecyclerView.ViewHolder{


        private final ImageView item2_iv;
        private final TextView item2_title;
        private final TextView item2_price;


        public Type2ViewHolder(View itemView) {
            super(itemView);
            item2_iv = itemView.findViewById(R.id.item2_iv);
            item2_title = itemView.findViewById(R.id.item2_title);
            item2_price = itemView.findViewById(R.id.item2_price);
        }
    }
}

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(configuration);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值