【达内课程】网络通信之HTTP协议(3)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010356768/article/details/81540411

修改MainActivity,登录成功后跳转收藏的店铺列表

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ......
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case ADD_CARD_SUCCESS:
                    Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    //界面跳转到CardListActivity
                    Intent intent = new Intent(MainActivity.this,ShopListActivity.class);
                    startActivity(intent);
                    break;
                case ADD_CARD_FAIL:
                    Toast.makeText(MainActivity.this,"登录失败:"+msg.obj,Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}

查看店铺接口
这里写图片描述

ShopListActivity

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ShopListActivity extends AppCompatActivity {
    private ListView listView;
    private ShopListAdapter adapter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case LOAD_SUCCESS:
                    Toast.makeText(ShopListActivity.this,"列表加载成功",Toast.LENGTH_SHORT).show();
                    //更新Adapter
                    setAdapter();
                    break;
                case LOAD_FAILED:
                    Toast.makeText(ShopListActivity.this,"失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    /**
     * 更新Adapter
     */
    private void setAdapter() {
        adapter = new ShopListAdapter(this, shopList);
        listView.setAdapter(adapter);
    }

    private static final int LOAD_SUCCESS = 1;
    private static final int LOAD_FAILED = 2;
    private List<ShopClass> shopList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_list);
        setViews();
        new Thread(){
            @Override
            public void run() {
                try {
                    loadCard();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    //在主线程中查询所有购物卡信息
    private void loadCard() throws IOException, JSONException {
        //1、URL
        URL url = new URL("http://域名/shop/street/shop-street-list");
        //2、HttpURLConnection
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
       //3、getInputStream
        InputStream is = conn.getInputStream();
        //4、获取JSON字符串
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = "";
        while ((line = reader.readLine())!=null){
            sb.append(line);
        }
        String json = sb.toString();
        //5、解析
        JSONObject obj = new JSONObject(json);
        String res = obj.getString("code");
        shopList = new ArrayList<>();
        if(res.equals("0")){
            //成功
            JSONArray array = obj.getJSONObject("data").getJSONArray("list");
            //封装数据
            for(int i=0;i<array.length();i++){
               JSONObject object = array.getJSONObject(i);
               ShopClass cardClass = new ShopClass();
               cardClass.setShop_id(object.getString("shop_id"));
               cardClass.setShop_name(object.getString("shop_name"));
               cardClass.setScore(object.getString("score"));
               cardClass.setCredit_name(object.getString("credit_name"));

               shopList.add(cardClass);
            }
        }
        //6、把数据显示到listview中
        handler.sendEmptyMessage(LOAD_SUCCESS);
    }

    private void setViews() {
        listView = findViewById(R.id.listView);
    }
}

activity_shop_list

<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=".ShopListActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

ShopListAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class ShopListAdapter extends BaseAdapter {
    private Context context;
    private List<ShopClass> list;
    private LayoutInflater inflater;

    public ShopListAdapter(Context context, List<ShopClass> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

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

    @Override
    public ShopClass getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if(view == null){
            view = inflater.inflate(R.layout.item_shop,null);
            holder = new ViewHolder();
            holder.shop_id = view.findViewById(R.id.shop_id);
            holder.shop_name = view.findViewById(R.id.shop_name);
            holder.credit_name = view.findViewById(R.id.credit_name);
            holder.score = view.findViewById(R.id.score);
            view.setTag(holder);
        }
        holder = (ViewHolder) view.getTag();
        //控件赋值
        //ShopClass card = list.get(i);
        ShopClass card = getItem(i);
        holder.shop_id.setText(card.getShop_id());
        holder.shop_name.setText(card.getShop_name());
        holder.credit_name.setText(card.getCredit_name());
        holder.score.setText(card.getScore());

        return view;
    }

    class ViewHolder{
        TextView shop_id;
        TextView shop_name;
        TextView credit_name;
        TextView score;
    }
}

item_shop

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ShopListActivity"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    >

    <TextView
        android:id="@+id/shop_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/shop_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/credit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10000"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_weight="1"/>

</LinearLayout>

ShopClass

public class ShopClass {
    public String shop_id;
    public String shop_name;
    public String credit_name;
    public String score;

    public ShopClass(String shop_id, String shop_name, String credit_name, String score) {
        this.shop_id = shop_id;
        this.shop_name = shop_name;
        this.credit_name = credit_name;
        this.score = score;
    }
    public ShopClass() {
    }

    public String getShop_id() {
        return shop_id;
    }

    public void setShop_id(String shop_id) {
        this.shop_id = shop_id;
    }

    public String getShop_name() {
        return shop_name;
    }

    public void setShop_name(String shop_name) {
        this.shop_name = shop_name;
    }

    public String getCredit_name() {
        return credit_name;
    }

    public void setCredit_name(String credit_name) {
        this.credit_name = credit_name;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }
}

猜你喜欢

转载自blog.csdn.net/u010356768/article/details/81540411