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>
阅读全文
0 0
- Httpurl网络获取数据
- 获取网络文件HttpUrl
- HTTPUrl-post网络请求数据
- HttpURL网络获取资源之:GET方法
- 【JavaEE小练手】Servlet实现HttpUrl数据获取
- 【Android小练手】安卓端实时获取HttpUrl数据
- HttpUrl --post请求数据
- HttpUrl--post请求数据
- 将网络(httpurl)图片转base64
- Android开发 通过httpURL获取图片
- httpclict和httpurl获取文字和图片
- 获取网络数据
- 获取网络数据
- 获取网络数据
- 网络交互,获取数据
- 网络数据获取
- 获取网络数据
- Android获取网络数据
- linux下分布式环境安装
- idea快捷键
- 身体各个时辰的变化
- 博客访问破万纪念
- TabLayout+ViewPager的基本使用
- Httpurl网络获取数据
- 实验3.5 十进制转换
- 从零开始实现线性判别分析(LDA)算法(多类情形)
- DBDT 基于不同损失函数的算法推导细节
- swift方法实现字符串的大小写转换
- Sap BussinessObjects Design Studio-infoChart
- 高手详解SQL性能优化十条经验
- Volume-Docker存储(一)
- JPA-Java Persistence API