xUtils框架网络获取
来源:互联网 发布:虚拟的社交知乎 编辑:程序博客网 时间:2024/06/06 05:47
1.导包和添加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.自己创个类继承Application
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this);//初始化xutils x.Ext.setDebug(BuildConfig.DEBUG);//是否输出debug日志,开启debug会影响性能 }}
3.在AndroidManifest里面使用
4.主函数
public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view) { switch (view.getId()){ case R.id.get_btn: getData(); break; case R.id.post_btn: postData(); break; } } //使用post请求获取网络数据 private void postData() { String path = "http://218.244.149.129:9010/api/companylist.php"; RequestParams params = new RequestParams(path); params.addBodyParameter("industryid", "98"); x.http().post(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Log.d("sxl", "onSuccess: "+result); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { Log.d("sxl", "onFinished: "); } }); } //使用get请求获取网络数据 public void getData() { String path = "http://218.244.149.129:9010/api/industry.php"; RequestParams params = new RequestParams(path); Callback.Cancelable cancelable = x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { // 该方法 是 成功 返回值 Log.d("TAG", "onSuccess: " + Thread.currentThread().getId() + "____s" + result); } @Override public void onError(Throwable ex, boolean isOnCallback) { // 错误的时候 Log.d("TAG", "onError: "); } @Override public void onCancelled(CancelledException cex) { // 取消的时候 Log.d("TAG", "onCancelled: "); } @Override public void onFinished() { //结束的时候 该方法 每次请求后 必然 执行的一个方法 Log.d("TAG", "onFinished: "); } }); cancelable.cancel();//取消此次网络访问 }}
5.listview适配器
public class MyAdapter extends BaseAdapter { private ArrayList<Data> list; private Context mContext; private LayoutInflater mInflater; public MyAdapter(ArrayList<Data> list, Context context) { this.list = list; mContext = context; mInflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHoler vh = null; if(view==null){ vh = new ViewHoler(); view = mInflater.inflate(R.layout.item,viewGroup,false); vh.pic_img = (ImageView) view.findViewById(R.id.img); vh.tv = (TextView) view.findViewById(R.id.title_tv); view.setTag(vh); }else { vh = (ViewHoler) view.getTag(); } Data data = (Data) getItem(i); //TODO 设置 return view; } class ViewHoler{ ImageView pic_img; TextView tv; }}
6.主布局
<?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.bw.alice.aday17xutils.MainActivity"> <Button android:id="@+id/get_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="get请求"/> <Button android:id="@+id/post_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="post请求"/> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv"></ListView></LinearLayout>
7.item布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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" tools:context="com.bw.alice.aday17xutils.MainActivity"> <ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="5dp"/> <TextView android:id="@+id/title_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/img" android:textSize="20sp"/></RelativeLayout>
阅读全文
0 0
- xUtils框架网络获取
- 网络访问框架XUtils
- android网络框架xutils
- Android-网络框架-XUtils
- Volley网络通信框架、XUtils
- Xutils框架之网络请求
- 使用网络XML,pull解析xUtils框架
- Android网络框架xUtils的数据库操作
- android okGo、okHttp、XUtils网络框架使用
- xutils网络框架的最简单使用
- Android Http请求框架二:xUtils 框架网络请求
- Android Http请求框架二:xUtils 框架网络请求
- Android 网络通信——xUtils网络框架
- Android网络框架xUtils的Http网络数据请求操作
- xUtils框架
- xUtils框架
- xUtils框架
- xUtils框架
- OkHttp框架
- 如何在html中获取字符串长度,扩展css对标签内容多省略
- eclipse打不开jsp(其他文件参考jsp)文件的解决方法
- CSS实现单行、多行文本溢出显示省略号
- MApp
- xUtils框架网络获取
- ES6 字符串扩展
- MyBatis和Hibernate相比,优势在哪里?
- activiti流程图查看
- Eclipse上Maven环境配置使用
- 2017精选BIM案例分享介绍及下载
- 【noip 2013】火柴排队
- [ Java学习 ] 一些Java程序 002
- Handler+ImageView控件实现定时换图片