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;
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;
List<PPBean> ppBeans = JSON.parseArray(s, PPBean.class);
mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecycleView.setAdapter(new MyppAdapter(ppBeans,getContext()));
}
}
};
public MyFragmengt2() {
}
@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;
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() {
}
@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;
}
}