Httpurl网络获取数据

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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值