okGo详细使用步骤(一)
来源:互联网 发布:薛之谦网络合唱丑八怪 编辑:程序博客网 时间:2024/05/22 09:44
OkGo的使用(一)
详细使用方式可以直接观看源文档wiki 这里不再说明
本文档也是依赖于源文档进行代码测试和理解写的。
写此文档时okgo版本 compile ‘com.lzy.net:okgo:3.0.4’
几个库的介绍
| library名 | 简介 | 作用 || ------------- |---------------| ------|| okgo | okhttp的封装库 | 访问网络 || okrx | 基于RxJava的扩展项目 | 实现异步操作,简单说切线程 || okrx2| 基于RxJava2的扩展项目 | 同上 || OkServer | okgo的扩展 | 上传、下载(支持断点续传) |
全局配置
我这边配置比较简单,需要详细配置参考源文档
写个类继承Application 然后在配置清单中添加
android:name=”.MyApp”
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:name=".MyApp" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity></application>
public class MyApp extends Application { @Override public void onCreate() { super.onCreate();// OkGo.getInstance().init(this); //最简单的配置 什么都不需要写 全部使用默认参数 OkHttpClient.Builder builder = new OkHttpClient.Builder();//可以使用OkGo内置的log拦截器打印log,如果你觉得不好用,也可以自己写个,这个没有限制。 HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");//log打印级别 loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);//log颜色级别 loggingInterceptor.setColorLevel(Level.INFO); builder.addInterceptor(loggingInterceptor); //全局的读取超时时间 builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);//全局的写入超时时间 builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);//全局的连接超时时间 builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS); /* * connectTimeout:指客户端和服务器 建立通道 的时间 writeTimeout:客户端把数据写出去需要的时间 readTimeout:客户端等待服务器返回数据的时间 * */ //使用内存保持cookie,app退出后,cookie消失 builder.cookieJar(new CookieJarImpl(new MemoryCookieStore())); OkGo.getInstance().init(this)//必须调用初始化 .setOkHttpClient(builder.build()) //建议设置OkHttpClient,不设置将使用默认的 .setRetryCount(1); //超时重连,本身1次,我这边设置1次,总共2次访问 }}
最基本的网络访问示例
本来想用公司的接口写个登录验证demo
不过还要看接口文档还要数据加密什么的 就简单的网上找了一个天气接口
示例中用了butterknife 你可以findviewbyid来找控件
public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";
private static String address = “http://www.weather.com.cn/data/sk/101010100.html“;
//@BindView(R.id.met_user)
//MaterialEditText metUser;
//@BindView(R.id.met_psd)
//MaterialEditText metPsd;
@BindView(R.id.submit)
Button submit;
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); long id = Thread.currentThread().getId(); Log.e(TAG,id+""); ButterKnife.bind(this);}@OnClick(R.id.submit)public void onViewClicked() { //String user = metUser.getText().toString(); //String psd = metPsd.getText().toString(); //if(user.isEmpty()||psd.isEmpty()){ // return; //} // TODO: 2017/8/14 提交数据给服务器 OkGo.<String>get(address) .execute(new StringCallback() { @Override public void onSuccess(Response<String> response) { long id = Thread.currentThread().getId();//从打印日志可以看到网络结果回调发生在主线程 线程id=1 Log.e(TAG,id+" : "+response.body()); } //这个方法默认是空实现,需要自己重写出来,不然你访问出了错你只能打印库中给你提供的打印日志 @Override public void onError(Response<String> response) { super.onError(response); } });}
}
运行完demo 点击一下button
E/MainActivity: 1 : {"weatherinfo":{"city":"北京","cityid":"101010100","temp":"18","WD":"东南风","WS":"1级","SD":"17%","WSE":"1","time":"17:05","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1011","rain"
阅读全文
1 0
- okGo详细使用步骤(一)
- 使用okgo的步骤
- git 使用总结之详细步骤一
- Okgo 使用三部曲
- OKGO使用,编写JSONCALLBACK
- OkGO的使用流程
- OKGO
- Androidstudio svn使用 版本控制 详细步骤(一)
- git 使用详细步骤
- TortoiseSVN使用详细步骤
- TortoiseSVN使用详细步骤
- CocoaPods详细使用步骤
- 使用ARM开发详细步骤
- 使用ARM开发详细步骤
- 使用ARM开发详细步骤
- 详细使用TortoiseSVN的步骤
- 使用springMVC的详细步骤
- 使用springMVC的详细步骤
- 我们爱分享----200多个js技巧代码
- msgServer(基于请求回应的网关服务器模板)
- jQuery学习笔记——jQuery选择器练习
- The AnimationClip 'xxx' used by the Animation component 'xxx' must be marked as Legacy.
- snapdragon camera menu控制与事件触发介绍
- okGo详细使用步骤(一)
- 字典树(前缀数)例题以及模板
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)
- 五个方法成为更好的程序员
- 浏览器同源政策及其规避方法
- win10安装scrapy
- SQLSERVER 存储过程分页算法
- 如何更改CPropertySheet的背景色
- zookeeper的安装目录调整