设置网络请求和网络传输

package com.example.day4_2;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpUtil {
    public static boolean isNotworkConnected(Context context){
        if (context!=null){
            ConnectivityManager mConnectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            mConnectivityManager.getActiveNetworkInfo();
            NetworkInfo mNetworkInfo=mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo!=null){
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
//====================================//====================================
    public static String requestString(String strUrl){
        try {
            URL url=new URL(strUrl);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode()==200){
                InputStream stream=connection.getInputStream();
                InputStreamReader inputStreamReader=new InputStreamReader(stream,"utf-8");
                BufferedReader reader=new BufferedReader(inputStreamReader);
                  //拼接字符串
                StringBuffer buffer=new StringBuffer();
                String str="";
                      //读取字符串
                while ((str=reader.readLine())!=null){
                 //拼接字符串
                    buffer.append(str);
                }
                return  buffer.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
}

////
////

package com.example.day4_2.frag;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.day4_2.HttpUtil;
import com.example.day4_2.R;
import com.example.day4_2.adapter.MyAdapter;
import com.example.day4_2.bean.JsonBean;
import com.google.gson.Gson;

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

public class Frag01 extends Fragment {
private ListView listView;
    String strURL = "https://www.apiopen.top/novelApi";

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

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        new Thread() {
            @Override
            public void run() {
                super.run();
                boolean isNet = HttpUtil.isNotworkConnected(getActivity());
                String s = "";
                if (isNet) {
                    s = HttpUtil.requestString(strURL);

                } else {

                }
                Message massage = new Message();
                massage.what = 0;
                massage.obj = s;
                handler.sendMessage(massage);
            }
        }.start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    String s=(String)msg.obj;
                    Gson gson = new Gson();
                    JsonBean jsonBean=gson.fromJson(s,JsonBean.class);
                    List<JsonBean.DataBean> list=jsonBean.getData();
                    listView.setAdapter(new MyAdapter(list,getActivity()));
                    break;
                //case 1:break;

            }
        }
    };
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值