OKhttp基本使用介绍
MainActivity.class
package com.example.administrator.okhttp3; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static OkHttpClient client = new OkHttpClient();
private Request request;
private Response response; private Button button1, button2, button3, button4;
private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btn_one);
button2 = (Button) findViewById(R.id.btn_two);
button3 = (Button) findViewById(R.id.btn_three);
button4 = (Button) findViewById(R.id.btn_four);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
textView = (TextView) findViewById(R.id.tv);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_one://同步get
client = new OkHttpClient();
request = new Request.Builder().url("http://cache.video.iqiyi.com/jp/avlist/202861101/1/?callback=jsonp9").build();
new Thread(new Runnable() {
@Override
public void run() {
try {
response = client.newCall(request).execute();
final String src = response.body().string();
Log.e("Tag", response.body().string());
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(src);
}
}); } catch (IOException e) {
e.printStackTrace();
}
}
}).start(); break;
case R.id.btn_two://异步get
request = new Request.Builder().url("http://cache.video.iqiyi.com/jp/avlist/202861101/1/?callback=jsonp9").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) { } @Override
public void onResponse(Call call, Response response) throws IOException {
final String src = response.body().string();
Log.e("Tag", response.body().string());
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(src);
}
});
}
});
break;
case R.id.btn_three://提交表单
RequestBody requestBody = new FormBody.Builder()
.add("search", "papap").build();
request = new Request.Builder().url("https://en.wikipedia.org/w/index.php").post(requestBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) { } @Override
public void onResponse(Call call, Response response) throws IOException {
final String src = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(src);
}
});
}
});
break;
case R.id.btn_four://文件下载
request = new Request.Builder().url("http://pic2.ooopic.com/10/18/01/04bOOOPICb3.jpg").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) { } @Override
public void onResponse(Call call, Response response) throws IOException {
InputStream inputStream = response.body().byteStream();
FileOutputStream fileOutputStream = new FileOutputStream(new File("/sdcard/tupian.jpg"));
byte[] buffer = new byte[2048];//每次循环读取2K的数据 int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.flush(); runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("文件下载成功。。。");
}
});
}
});
break;
}
}
}
PS:需要注意的是,在进行同步Get网络请求时,因为都是一个耗时操作,所以需要建立一个子线程去进行请求; 并且,UI界面的更新,必须放在主线程去完成!
activity_main.xml
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.okhttp3.MainActivity"> <Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="同步get" /> <Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="异步get" /> <Button
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post提交表单" /> <Button
android:id="@+id/btn_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件下载" /> <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"/>
</LinearLayout>


OKhttp基本使用介绍的更多相关文章
- OKHttp使用简单介绍
如今android网络方面的第三方库非常多,volley.Retrofit.OKHttp等,各有各自的特点,这边博客就来简介下怎样使用OKHttp. 梗概 OKHttp是一款高效的HTTP客户端,支持 ...
- Android okHttp网络请求之Retrofit+Okhttp+RxJava组合
前言: 通过上面的学习,我们不难发现单纯使用okHttp来作为网络库还是多多少少有那么一点点不太方便,而且还需自己来管理接口,对于接口的使用的是哪种请求方式也不能一目了然,出于这个目的接下来学习一下R ...
- Android OkHttp使用与分析
安卓开发领域,很多重要的问题都有了很好的开源解决方案,例如网络请求 OkHttp + Retrofit 简直就是不二之选."我们不重复造轮子不表示我们不需要知道轮子该怎么造及如何更好的造!& ...
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
目录 前言 1 Get方式和Post方式接口说明 2 OkHttp库简单介绍及环境配置 3 具体实现 前言 本文具体实现思路和大部分代码参考自<第一行代码>第2版,作者:郭霖:但是文中讲 ...
- Android的OkHttp开源框架的使用方法
前段时间研究了下Android里面非常火爆的网络请求库OkHttp,这篇文章主要来介绍下OkHttp的常用请求的使用方式,后面一篇文章会介绍本人基于OkHttp封装的一个操作更简单.更适用于项目的网络 ...
- OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据
OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据 我们这片博文就来聊聊这个反响很不错的OkHttp了,标题是我恶搞的,本篇将着重详细的 ...
- OKHttp概览
1,整体思路 从使用方法出发,首先是怎么使用,其次是我们使用的功能在内部是如何实现的,实现方案上有什么技巧,有什么范式.全文基本上是对 OkHttp 源码的一个分析与导读,非常建议大家下载 OkHtt ...
- 【转载】okhttp源码解析
转自:http://www.open-open.com/lib/view/open1472216742720.html https://blog.piasy.com/2016/07/11/Unders ...
- Android Okhttp POST提交键值对
以前的项目网络连接那块一直坚持使用HttpClient,总是会出现一些莫名奇妙的问题,现在新的项目使用了OKHttp网络框架,发现超级好用,上网再了解下,发现OkHttp口碑真的不错,对比之下Http ...
随机推荐
- 再谈 X-UA-Compatible 兼容模式
如何理解 IE 的文档兼容模式(X-UA-Compatible)? IE 浏览器支持多种文档兼容模式,得以因此改变页面的渲染效果. IE9 模式支持全范围的既定行业标准,包括 HTML5(草案), W ...
- 【bzoj1596】[Usaco2008 Jan]电话网络
题目描述 Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无线电通讯塔,来 ...
- Jetty安装
下载jetty http://www.eclipse.org/jetty/ 看好jdk 版本 安装 解压压缩包到指定目录,且将其目录路径定义为${JETTY_HOME} 进入${JETTY_HOME ...
- Ubuntu 14 常用“快捷键”,Ctrl + Alt + F1 进入终端,按 Ctrl + Alt + F7 回到界面
Ubuntu中所谓 Super键,就是 Windows建,一般在键盘的 ctrl 和 alt 2个键之间,一个微软窗口的图标. 1.持续按住 Super键,会弹出“键盘快捷键”大全: 2.修改快捷键路 ...
- 分分钟教会大家第一个Spring入门案例
1.下载Spring jar包,并添加到项目中. 官网地址http:springsource.org 2.在项目中新建一个类 package cn.test; public class He ...
- 怎样用Lodrunner测试WAP站点的性能(两种解决方案)
其实用IE就可以的!!!! 1.借助opera实现对WAP站点的录制 第一:安装opera软件 第二:Lodrunner选择Web(HTTP/HTML)协议 第三:Lodrunner的Applicat ...
- php数据结构与算法
php面试题之二--数据结构和算法(高级部分) 二.数据结构和算法 1.使对象可以像数组一样进行foreach循环,要求属性必须是私有.(Iterator模式的PHP5实现,写一类实现Iterator ...
- Ajax前台调用后台方法、AJAX Pro2(回调函数)
//获取分店 function cityResult() { if (cityName != "") { $("#ddlcity_").find("o ...
- nginx反向代理、优化
本优化适合apache,nginx,squid多种等web应用,特殊的业务也可能需要略作调.生产环境linux的内核优化 net.ipv4.tcp_fin_timeout = net.ipv4.tcp ...
- opencv删除二值图中较小的噪点色块
CvSeq* contour = NULL; double minarea = 100.0; double tmparea = 0.0; CFileDialog dlg(true); if (dlg. ...