Httpurl网络获取数据

来源:互联网 发布:python网络编程 amazon 编辑:程序博客网 时间:2024/05/01 18:59

1.获取网络访问权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>



2.主布局

public class MainActivity extends AppCompatActivity {    private ListView lv;    private String str="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";    private StringBuilder s;    private ArrayList<stl> arr;    private ada ad;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lv=(ListView)findViewById(R.id.lv);        arr=new ArrayList<>();        ad= new ada(this, arr);        lv.setAdapter(ad);    }    public void cc(View view) {          new http().start();    }    class http extends Thread{        private BufferedInputStream buff;        private HttpURLConnection http;        @Override        public void run() {            super.run();            try {                URL url=new URL(str);                http = (HttpURLConnection) url.openConnection();                http.setConnectTimeout(3000);                http.connect();                if(http.getResponseCode()==200){                    buff=new BufferedInputStream(http.getInputStream());                    s=new StringBuilder();                    byte[] b=new byte[1024];                    int len=0;                    while ((len=buff.read(b))!=-1){                           s.append(new String(b,0,len));                    }                    arr.clear();                    JSONObject json1 = new JSONObject(s.toString());                    JSONArray json2 = json1.getJSONArray("data");                    for (int i = 0; i <json2.length() ; i++) {                        JSONObject json3 = json2.getJSONObject(i);                        String title = json3.getString("title");                        String pic = json3.getString("pic");                        stl st = new stl(title, pic);                        arr.add(st);                    }                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                         ad.notifyDataSetChanged();                        }                    });                 }            } catch (MalformedURLException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            } catch (JSONException e) {                e.printStackTrace();            } finally {                if(buff!=null){                    try {                        buff.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }                if(http!=null){                    http.disconnect();                }            }        }    }}




3.实体类

public class stl {    private String title;    private String pic;    public stl(String title, String pic) {        this.title = title;        this.pic = pic;    }    @Override    public String toString() {        return "stl{" +                "title='" + title + '\'' +                ", pic='" + pic + '\'' +                '}';    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getPic() {        return pic;    }    public void setPic(String pic) {        this.pic = pic;    }}




4.适配器

public class ada extends BaseAdapter {    private ArrayList<stl> arr;    private Context co;    public ada(Context co, ArrayList<stl> arr) {        this.co = co;        this.arr = arr;    }    @Override    public int getCount() {        return arr.size();    }    @Override    public stl getItem(int i) {        return arr.get(i);    }    @Override    public long getItemId(int i) {        return i;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        youhua you;        if(view==null){            view = View.inflate(co, R.layout.stl, null);            you = new youhua();            you.tv=view.findViewById(R.id.tv);            you.iv=view.findViewById(R.id.iv);            view.setTag(you);        }else {            you= (youhua) view.getTag();        }        stl item = getItem(i);        you.tv.setText(item.getTitle());        Picasso.with(co).load(item.getPic()).into(you.iv);        return view;    }    class youhua{        private TextView tv;        private ImageView iv;    }}




5.主布局

<?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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.myapplication.MainActivity"    android:orientation="vertical">    <Button        android:id="@+id/but"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="获取"        android:onClick="cc"/>    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>




6.item布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    >   <TextView       android:id="@+id/tv"       android:layout_width="wrap_content"       android:layout_height="wrap_content" />    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="50dp" /></LinearLayout>


原创粉丝点击