Android-开源框架之XUtils的基本使用
来源:互联网 发布:sql union 重复 编辑:程序博客网 时间:2024/06/06 16:26
今天介绍一些Android框架里面的网络框架,我认为功能比较强大的XUtils框架
目前XUtils里面总共有四大模块:DbUtils数据库模块、ViewUtils UI模块、HttpUtil网络模块、BitmapUtil图片加载模块
使用XUtils3步骤
1).先去官网下载jar包
2).复制到项目的libs下
3).添加权限
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
4).在Gradle里添加依赖
compile 'org.xutils:xutils:3.3.40'5).我们新建一个MyApplication类,继承Application,重写onCreat方法,在里面初始化XUtils
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); //初始化 x.Ext.init(this); }}
然后要记得去Manifest里面添加类声明
然后去写布局文件
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="xutils获取数据" android:id="@+id/b_xutils_data" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_utils_show" /> </ScrollView>
在Activity中,我们要使用注解的方式去获取控件
package com.zking.android_parserjson;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import org.xutils.common.Callback;import org.xutils.http.RequestParams;import org.xutils.view.annotation.Event;import org.xutils.view.annotation.ViewInject;import org.xutils.x;public class xutilsActivity extends AppCompatActivity { @ViewInject(R.id.tv_utils_show) private TextView tv_utils_show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xutils); //加载view注解 x.view().inject(this); } @Event(value=R.id.b_xutils_data,type=View.OnClickListener.class) private void getData(View view){ //获取数据 RequestParams params=new RequestParams("https://www.baidu.com/s"); params.addQueryStringParameter("wd","2017世界编程语言"); x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { tv_utils_show.setText(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { tv_utils_show.setText("网络君感冒了"); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); }}
因为是获取百度上的数据,所以编译出来是这个样的,但是,也表明我们获取成功了
然后我们再试一下获取百度的logo图片
在布局文件里面写一个ImageButton,设置一个默认图片
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ib_xutils_image" android:src="@mipmap/ic_launcher" />
在Activity里面,得到控件
@ViewInject(R.id.ib_xutils_image) private ImageView ib_xutils_image;
给控件设值
x.image().bind(ib_xutils_image,"https://www.baidu.com/img/bd_logo1.png");
然后就可以看到我们的效果图了
0 0
- Android-开源框架之XUtils的基本使用
- android多线程下载开源框架xutils的使用
- android xutils框架的使用
- XUtils框架基本使用
- Android开发框架xUtils之ViewUtils的使用
- 开源框架xUtils的使用
- 常见Android开源框架使用笔记之——xUtils框架
- Android Xutils框架使用之HttpUtils
- Android Xutils框架使用之BitmapUtils
- Android Xutils框架使用之DBUtils
- Android Xutils框架使用之ViewUtils
- android框架之XUtils
- Android 框架之xUtils
- 开源框架之xUtils
- Android Xutils框架使用
- android 开源框架xUtils
- android 开源框架xUtils
- android 开源框架xUtils
- 【C语言简单说】十四:for循环
- code vs 1735 方程的解数 (dfs+hash)
- scrapy定时执行抓取任务
- Hadoop实践(零)---Hadoop作为数据湖
- java数组逆序用for循环,for循环的初始化语句定义了两个变量
- Android-开源框架之XUtils的基本使用
- Java集合系列(10)--TreeSet
- poj Babelfish(二分||map)
- Java Fundamental day03
- 40、Linux初步(CentOS 7下安装配置FTP)
- Hibernate核心API的详解
- Android 官方示例:android-architecture 学习笔记(五)之todo-mvp-clean
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- 如何高效学习