在grandle文件中添加
compile 'org.xutils:xutils:3.3.30'
1)加入网络权限、sdcard权限
compile 'org.xutils:xutils:3.3.30'
1)加入网络权限、sdcard权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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); x.Ext.setDebug(false); //输出debug日志,开启会影响性能 }}
3)在AndroidManifest文件中注册MyApp
<application android:name=".MyApp" ...</application>
4)Activity的注解的使用如下:
@ContentView(R.layout.activity_main)public class MainActivity extends AppCompatActivity { @ViewInject(R.id.viewpager) ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); x.view().inject(this); ... }}
5)GET请求
RequestParams params = new RequestParams(url);params.addQueryStringParameter("username","abc");params.addQueryStringParameter("password","123");x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { //解析result } //请求异常后的回调方法 @Override public void onError(Throwable ex, boolean isOnCallback) { } //主动调用取消请求的回调方法 @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { }});