布局什么的自己开发
netUtil
public class Netb {
private static String s;
public static List<Bean.DataBean> getHttpClient(String urlString) {
//获取请求
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//创建请求方式,接口传入
HttpGet httpGet = new HttpGet(urlString);
try {
//请求执行接口,返回的是服务器响应
HttpResponse response = defaultHttpClient.execute(httpGet);
//获取执行的状态码
int statusCode = response.getStatusLine().getStatusCode();
//判断状态码
if(statusCode == 200)
{
//获取数据
//entity包含服务器返回的数据
HttpEntity entity = response.getEntity();
//Entity用来专门处理entity
s = EntityUtils.toString(entity);
}
//开始解析
Gson gson = new Gson();
Bean json = gson.fromJson(s, Bean.class);
List<Bean.DataBean> data = json.getData();
return data;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
这又是一个网址
public class Frame_02 extends Fragment {
String urlString = “http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
String urlBitmap = “https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg”;
private View view;
Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 1)
{
List<Bean.DataBean> list = (List<Bean.DataBean>) msg.obj;
//获取listview组件
ListView listView2 = view.findViewById(R.id.listView2);
//创建适配器
MyAdapterb myAdapterb = new MyAdapterb(getActivity(), list);
//设置适配器
listView2.setAdapter(myAdapterb);
}
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frame_02, container, false);
new Thread()
{
@Override
public void run() {
super.run();
List<Bean.DataBean> list = Netb.getHttpClient(urlString);
Message message = handler.obtainMessage();
message.what = 1;
message.obj = list;
handler.sendMessage(message);
}
}.start();
return view;
}
}
适配器必不可少
class MyAdapterb extends BaseAdapter {
private Context context;
private List<Bean.DataBean> list;
public MyAdapterb(Context context, List<Bean.DataBean> 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) {
ViewHolderb vh = null;
if(convertView == null)
{
convertView = View.inflate(context, R.layout.listviewb, null);
vh = new ViewHolderb();
vh.textViewa = convertView.findViewById(R.id.textViewa);
vh.textViewb = convertView.findViewById(R.id.textViewb);
vh.textViewc = convertView.findViewById(R.id.textViewc);
vh.textViewd = convertView.findViewById(R.id.textViewd);
convertView.setTag(vh);
}
else
{
vh = (ViewHolderb) convertView.getTag();
}
Bean.DataBean dataBean = list.get(position);
vh.textViewa.setText(dataBean.getPic_url());
vh.textViewb.setText(dataBean.getNews_summary());
vh.textViewc.setText(dataBean.getNews_title());
vh.textViewd.setText(dataBean.getNews_id());
return convertView;
}
}
class ViewHolderb
{
public TextView textViewa;
public TextView textViewb;
public TextView textViewc;
public TextView textViewd;
}