Handler与Thread

package com.example.ninetwohomework.thread;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

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

public class ProviderThread extends Thread{

    private static final String TAG = "ProviderThread";
    private String uu;
    private int type;
    private Handler handler;

    public ProviderThread(String uu, int type, Handler handler) {
        this.uu = uu;
        this.type = type;
        this.handler = handler;
    }

    @Override
    public void run() {
        super.run();

        try {
            URL url = new URL("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&" + uu);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            Log.i(TAG, "run: "+"我进来了");
            httpURLConnection.connect();
            Log.i(TAG, "run: "+"我进来了");

            if(httpURLConnection.getResponseCode()==200){
                InputStream is = httpURLConnection.getInputStream();
                int len=0;
                byte[] bytes = new byte[1024];
                StringBuffer sb = new StringBuffer();
                while((len=is.read(bytes)) != -1){
                    sb.append(new String(bytes,0,len));
                }
                if(type==1){
                    Message message = new Message();
                    message.what=110;
                    message.obj=sb.toString();
                    handler.sendMessage(message);
                }else if(type==2){
                    Message message = new Message();
                    message.what=140;
                    message.obj=sb.toString();
                    handler.sendMessage(message);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package com.example.ninetwohomework.fragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.example.ninetwohomework.R;
import com.example.ninetwohomework.adapter.MyppAdapter;
import com.example.ninetwohomework.bean.PPBean;
import com.example.ninetwohomework.thread.ProviderThread;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragmengt2 extends Fragment {

    private RecyclerView mRecycleView;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==140){
                String s = (String) msg.obj;
//                JSONArray jsonArray = JSONArray.parseArray(s);
//                for (Iterator iterator = jsonArray.iterator(); iterator.hasNext();) {
//                    HashMap<String, String> map = new HashMap<>();
//                    JSONObject job=(JSONObject)iterator.next();
//                    String strings = job.get("thumb").toString();
//                    String name = job.get("title").toString();
//                    map.put("pic",strings);
//                    map.put("name",name);
//                    mapList.add(map);
//                    Log.i(TAG, "handleMessage: "+map.toString());
                List<PPBean> ppBeans = JSON.parseArray(s, PPBean.class);
                mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
                mRecycleView.setAdapter(new MyppAdapter(ppBeans,getContext()));
            }
        }
    };
    public MyFragmengt2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_my_fragmengt2, container, false);

        mRecycleView = inflate.findViewById(R.id.recycleView);

        new ProviderThread("catid=&act=list&mid=13&orderby=desc&page=1&kw=&pagenum=10&msgtypeid=3&ordertype=addtime",
                2,handler).start();
        return inflate;
    }

}
package com.example.ninetwohomework.fragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.ninetwohomework.R;
import com.example.ninetwohomework.adapter.MyListViewAdapter;
import com.example.ninetwohomework.bean.ProviderBean;
import com.example.ninetwohomework.thread.ProviderThread;
import com.google.gson.Gson;

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

/**
 * A simple {@link Fragment} subclass.
 */
public class Myfragment extends Fragment {

    private static final String TAG = "Myfragment";
    private ListView mListView;
    private MyListViewAdapter adapter;
    private List<ProviderBean> list=new ArrayList<>();
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==110){
                String s = (String) msg.obj;
                Log.i(TAG, "handleMessage: "+s);
                Gson gson = new Gson();
                ProviderBean[] providerBeans = gson.fromJson(s, ProviderBean[].class);
                for (ProviderBean providerBean : providerBeans) {
                    list.add(providerBean);
                }
                adapter.notifyDataSetChanged();
            }
        }
    };

    public Myfragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_myfragment, container, false);
        mListView = inflate.findViewById(R.id.listView);

        new ProviderThread("mid=5&orderby=desc&kw=&pagenum=10&msgtypeid=3&catid=&act=list&areaid=&brandid=&appauth=6b84wrbLGCVtH5ErLgqmzkvarQqFOqdT6YSQ-S-4vLn6LbEJ6O-P-X-P-PooZRJMu3sLbmw-S-FsG3oIrbSRS4WSuoOHRdG7ccHCoQ&typeid=&page=1&ordertype=addtime",
                1,handler).start();
        adapter=new MyListViewAdapter(getContext(),list);
        mListView.setAdapter(adapter);
        return inflate;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值