HTTPURLConnection與Handle結合使用
阿新 • • 發佈:2018-11-21
MainActivity
import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ListView; import com.example.dell_pc.myapplication.R; import com.example.dell_pc.myapplication.bean.Bean1; import com.example.dell_pc.myapplication.bean.MyAdapter; import com.example.dell_pc.myapplication.util.HttpUtils; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class ListFragment extends Fragment implements View.OnClickListener { private Button sendBtn; private ListView mListView; private MyAdapter myAdapter; private List<Bean1.DataBean> data=new ArrayList<>(); private String mUrl = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1"; private Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case 0: data.addAll((List<Bean1.DataBean>)msg.obj); myAdapter.notifyDataSetChanged(); break; } } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_list, container, false); sendBtn = v.findViewById(R.id.Send_Btn); sendBtn.setOnClickListener(this); mListView = v.findViewById(R.id.List_View); return v; } private void getNetData() { new Thread(new Runnable() { @Override public void run() { try { String str = HttpUtils.get(mUrl); Gson gson = new Gson(); Bean1 bean1 = gson.fromJson(str, Bean1.class); mHandler.sendMessage(mHandler.obtainMessage(0, bean1.getData())); } catch (Exception e) { e.printStackTrace(); } } }).start(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.Send_Btn: getNetData(); myAdapter = new MyAdapter(getActivity(), data); mListView.setAdapter(myAdapter); break; } } }
HttpUtil工具類
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtils { public static String get(String mUrl) throws IOException { URL url = new URL(mUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); InputStream stream = connection.getInputStream(); String str = getInPutStr(stream); return str; } private static String getInPutStr(InputStream stream) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); StringBuffer buffer = new StringBuffer(); String con = null; while ((con = reader.readLine()) != null) { buffer.append(con); } return buffer.toString(); } }
Adapter封裝類
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.example.dell_pc.myapplication.R; import java.util.List; public class MyAdapter extends BaseAdapter { private Context mContext; private List<Bean1.DataBean> data; public MyAdapter(Context mContext, List<Bean1.DataBean> data) { this.mContext = mContext; this.data = data; } public void setData(List<Bean1.DataBean> data) { this.data = data; notifyDataSetChanged(); } @Override public int getCount() { return data.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) { ViewHolder viewHolder = null; if (convertView==null){ convertView = View.inflate(mContext, R.layout.item, null); viewHolder = new ViewHolder(); viewHolder.Text_Title=convertView.findViewById(R.id.Text_Title); convertView.setTag(viewHolder); }else{ viewHolder =(ViewHolder) convertView.getTag(); } viewHolder.Text_Title.setText(data.get(position).getNews_title()); return convertView; } class ViewHolder{ TextView Text_Title; } }
Bean類
import java.util.List;
public class Bean1 {
private int status;
private String info;
private List<DataBean> data;
public int getStatus() {
return status;
}
public String getInfo() {
return info;
}
public List<DataBean> getData() {
return data;
}
public void setStatus(int status) {
this.status = status;
}
public void setInfo(String info) {
this.info = info;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* news_id : 13811
* news_title : 深港澳臺千里連線,嘉年華會今夏入川
* news_summary : 6月17—20日,“2016成都深港澳臺嘉年華會”(簡稱嘉年華會)將在成都世紀城國際會展中心舉辦。其主辦方勵展華博借力旗
* pic_url : http://f.expoon.com/sub/news/2016/01/21/887844_230x162_0.jpg
*/
private String news_id;
private String news_title;
private String news_summary;
private String pic_url;
public String getNews_id() {
return news_id;
}
public void setNews_id(String news_id) {
this.news_id = news_id;
}
public String getNews_title() {
return news_title;
}
public void setNews_title(String news_title) {
this.news_title = news_title;
}
public String getNews_summary() {
return news_summary;
}
public void setNews_summary(String news_summary) {
this.news_summary = news_summary;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".com.bawei.fragment.ListFragment">
<Button
android:id="@+id/Send_Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/hello_blank_fragment" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/List_View"></ListView>
</LinearLayout>