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 ...
随机推荐
- 升级xcode7的问题:使用shareSDK,坑的你两眼泪汪汪
升级xcode之前好好的一个项目,升级后就crash,错误直接定位到main函数,报的是EXC_BAD_ACCESS错误,内存错误,就是一个对象释放了,继续对他发消息就会报错.详细定位错误,就是定位不 ...
- LESS介绍及其与Sass的差异
自从一个月前我偶然发现LESS之后我就开始坚定的使用它了.CSS本身对我来说从来不是问题,但是我很好奇使用变量来沿着一个调色盘为我的网站或模板创建一些东西的想法.拥有一个提供固定数量选项可选的色盘可以 ...
- C++Socket编程总结 [转]
使用socket写代码主要是要看自己的需求是什么. 如果通信时,内容很重要就要使TCP方式. 如果用户数太多,可能就要使用UDP方式了. 在TCP模式下,最简单的方式就是这样的,使阻塞方式: 服务端: ...
- 爆料喽!!!开源日志库Logger的剖析分析
导读 Logger类提供了多种方法来处理日志活动.上一篇介绍了开源日志库Logger的使用,今天我主要来分析Logger实现的原理. 库的整体架构图 详细剖析 我们从使用的角度来对Logger库抽茧剥 ...
- ios中二维码的使用之一: 二维码的生成
iOS在7之后,具备了原生的二维码生成API; 生成二维码的准备: #import <CoreImage/CoreImage.h> 导入框架: 开始生成: //1- 创建过滤器 CIFi ...
- ACCESS延时注入
这也算是个新知识吧.今天遇到个站,实实在在存在注入,但是弄不出字段.本应该可以用便宜注入.但是不知道为什么就是就是弄不出来. 大家如果有兴许可以加学习交流群:281245781 交流一下吧. Payl ...
- qt-5.6.0 移植之qt文件系统的建立
经过差不多两个星期的奋斗,终于在板子里面跑起来了qt 程序,虽然现在还没有把触摸屏驱动加上去,但是我相信已经不远了!!!!! 在前两篇的随笔里面 , 已经编译好了最纯净的文件系统以及交叉编译完成了qt ...
- CCF 模拟E DFS深搜
http://115.28.138.223:81/view.page?opid=5 这道题问的很怪. 起点DFS,每一个点还要DFS一次,统计不能到终点的个数 数据量不大这样做也能AC #includ ...
- idea 排除编译文件,恢复编译
- ubuntu 13.10 skype登不上问题
首先打开sources.list sudo gedit /etc/apt/sources.list 如果是13.10添加源: deb http://archive.canonical.com/ubun ...