HttpContent

package com.daydayup.zhoukao1.acitivities;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.daydayup.zhoukao1.R;
import com.daydayup.zhoukao1.fragments.Fragment01;
import com.daydayup.zhoukao1.fragments.Fragment02;

/**
 *
 *
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout content;
    private Fragment01 fragment01;
    private Fragment02 fragment02;

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

        //1.初始化界面
        initViews();

    }

    private void initViews() {
        TextView one = findViewById(R.id.one);
        TextView two = findViewById(R.id.two);
        content = findViewById(R.id.content);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        //3.创建Fragment,添加到容器。
        fragment01 = new Fragment01();
        fragment02 = new Fragment02();

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.content, fragment01);
        transaction.commit();//事务每次只能提交一次

    }

    //2.点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.one:
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                transaction.replace(R.id.content, fragment01);
                transaction.commit();//事务每次只能提交一次
                break;

            case R.id.two:
                FragmentManager fm1 = getSupportFragmentManager();
                FragmentTransaction transaction1 = fm1.beginTransaction();
                transaction1.replace(R.id.content, fragment02);
                transaction1.commit();//事务每次只能提交一次
                break;
        }
    }
}


public class Fragment01 extends Fragment {
    private static final String TAG = "Fragment01----";
    private MyHandler myHandler = new MyHandler();
    private final static int SUCCESS = 0;
    private final static int ERROR = 1;
    private ListView listView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment01_layout, container, false);
        listView = view.findViewById(R.id.fragment01_listview);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //4.获取网络数据
        new Thread() {
            @Override
            public void run() {
                try {
                    URL u = new URL(HttpConfig.ONE_URL);
                    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
                    connection.setConnectTimeout(5000);
                    if (connection.getResponseCode() == 200) {
                        InputStream inputStream = connection.getInputStream();
                        //5.封装工具类
                        String json = CommenUtils.inputStream2String(inputStream);
                        //6.使用Handler,发送数据
                        Message message = myHandler.obtainMessage();
                        message.what = SUCCESS;
                        message.obj = json;
                        myHandler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    //成功
                    String json = (String) msg.obj;
                    Log.d(TAG, "handleMessage: " + json);
                    //导包Gson
                    Gson gson = new Gson();
                    //如果json就是一个纯属组的解析方式
                    Type listType = new TypeToken<LinkedList<NewsBean>>(){}.getType();

                    LinkedList<NewsBean> list = gson.fromJson(json, listType);//7.根据接口,生成javabean

                    //8.书写Adapter进行显示
                    List<NewsBean.ItemBean> item = list.get(0).getItem();

                    MyAdapter myAdapter = new MyAdapter(getActivity(), item);

                    listView.setAdapter(myAdapter);

                    break;
            }
        }
    }

}

public class Fragment02 extends Fragment {
    private static final String TAG = "Fragment02";
    private TextView name, address;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment02_layout, container, false);
        name = view.findViewById(R.id.fragment02_name);
        address = view.findViewById(R.id.fragment02_add);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //1.请求网络
        /**
         * 记得要添加HttpClient的依赖库  百度
         */
        new MyTask().execute(HttpConfig.TWO_URL);
    }


    class MyTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
            String url = strings[0];
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            //执行
            try {
                HttpResponse response = client.execute(get);
                //状态码
                StatusLine statusLine = response.getStatusLine();
                if (statusLine.getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream inputStream = entity.getContent();
                    //将流转成String
                    String json = CommenUtils.inputStream2String(inputStream);
                    Log.d(TAG, "run: " + json);
                    //发送
                    return json;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            UserBean userBean = gson.fromJson(s, UserBean.class);
            //赋值
            name.setText(userBean.getData().getName());
            address.setText(userBean.getData().getAddr());
        }
    }

}

public class MyAdapter extends BaseAdapter {

    private Context context;
    private List<NewsBean.ItemBean> list;

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

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder=null;
        if (convertView==null){
            convertView=View.inflate(context, R.layout.fragment01_item,null);
            CircleImageView circleImageView=convertView.findViewById(R.id.item_pic);
            TextView textView=convertView.findViewById(R.id.item_title);
            myViewHolder=new MyViewHolder(circleImageView,textView);

            convertView.setTag(myViewHolder);

        }
        else {
            myViewHolder= (MyViewHolder) convertView.getTag();
        }
        //赋值
        myViewHolder.getTv().setText(list.get(position).getTitle());
        String thumbnail = list.get(position).getThumbnail();
        Glide.with(context).load(thumbnail).into(myViewHolder.getPic());
        return convertView;
    }

    class MyViewHolder {
        private CircleImageView pic;
        private TextView tv;

        public MyViewHolder(CircleImageView pic, TextView tv) {
            this.pic = pic;
            this.tv = tv;
        }

        public CircleImageView getPic() {
            return pic;
        }

        public void setPic(CircleImageView pic) {
            this.pic = pic;
        }

        public TextView getTv() {
            return tv;
        }

        public void setTv(TextView tv) {
            this.tv = tv;
        }
    }
}
package com.daydayup.zhoukao1.utils;

import java.io.IOException;
import java.io.InputStream;

/**
 * 公共的工具类
 */
public class CommenUtils {
    //将输入流转成字符串
    public static String inputStream2String(InputStream inputStream) throws IOException {
        int len = 0;
        byte[] butter = new byte[1024];
        StringBuffer stringBuffer = new StringBuffer();
        while ((len = inputStream.read(butter)) != -1) {
            String s = new String(butter, 0, len,"UTF-8");
            stringBuffer.append(s);
        }
        return stringBuffer.toString();
    }
}

<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=".acitivities.MainActivity">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"
        android:background="#999999" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="页面一"
            android:textSize="30sp" />

        <View
            android:layout_width="0.75dp"
            android:layout_height="match_parent"></View>

        <TextView
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="页面二"
            android:textSize="30sp" />

    </LinearLayout>

</LinearLayout>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/item_pic"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="标题"
        android:textSize="20sp" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="页面二"
        android:textColor="#000"
        android:textSize="30sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"
        android:background="#999999" />

    <TextView
        android:id="@+id/fragment02_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/fragment02_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值