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基本使用介绍的更多相关文章

  1. OKHttp使用简单介绍

    如今android网络方面的第三方库非常多,volley.Retrofit.OKHttp等,各有各自的特点,这边博客就来简介下怎样使用OKHttp. 梗概 OKHttp是一款高效的HTTP客户端,支持 ...

  2. Android okHttp网络请求之Retrofit+Okhttp+RxJava组合

    前言: 通过上面的学习,我们不难发现单纯使用okHttp来作为网络库还是多多少少有那么一点点不太方便,而且还需自己来管理接口,对于接口的使用的是哪种请求方式也不能一目了然,出于这个目的接下来学习一下R ...

  3. Android OkHttp使用与分析

    安卓开发领域,很多重要的问题都有了很好的开源解决方案,例如网络请求 OkHttp + Retrofit 简直就是不二之选."我们不重复造轮子不表示我们不需要知道轮子该怎么造及如何更好的造!& ...

  4. 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据

     目录 前言 1 Get方式和Post方式接口说明 2 OkHttp库简单介绍及环境配置 3 具体实现 前言 本文具体实现思路和大部分代码参考自<第一行代码>第2版,作者:郭霖:但是文中讲 ...

  5. Android的OkHttp开源框架的使用方法

    前段时间研究了下Android里面非常火爆的网络请求库OkHttp,这篇文章主要来介绍下OkHttp的常用请求的使用方式,后面一篇文章会介绍本人基于OkHttp封装的一个操作更简单.更适用于项目的网络 ...

  6. OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据

    OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据 我们这片博文就来聊聊这个反响很不错的OkHttp了,标题是我恶搞的,本篇将着重详细的 ...

  7. OKHttp概览

    1,整体思路 从使用方法出发,首先是怎么使用,其次是我们使用的功能在内部是如何实现的,实现方案上有什么技巧,有什么范式.全文基本上是对 OkHttp 源码的一个分析与导读,非常建议大家下载 OkHtt ...

  8. 【转载】okhttp源码解析

    转自:http://www.open-open.com/lib/view/open1472216742720.html https://blog.piasy.com/2016/07/11/Unders ...

  9. Android Okhttp POST提交键值对

    以前的项目网络连接那块一直坚持使用HttpClient,总是会出现一些莫名奇妙的问题,现在新的项目使用了OKHttp网络框架,发现超级好用,上网再了解下,发现OkHttp口碑真的不错,对比之下Http ...

随机推荐

  1. 移动端 css实现自适应正圆 ( 宽高随着手机屏幕宽度自适应 )

    序言:应朋友要求随手写了一下移动端 css实现自适应正圆 ( 宽高随着手机屏幕宽度自适应 ) ,以备后用 LESS代码: .adaptive-circle { margin: 50px auto 0; ...

  2. 使用 IntelliJ IDEA 导入 Spark源码及编译 Spark 源代码

    1. 准备工作 首先你的系统中需要安装了 JDK 1.6+,并且安装了 Scala.之后下载最新版的 IntelliJ IDEA 后,首先安装(第一次打开会推荐你安装)Scala 插件,相关方法就不多 ...

  3. 【PHP面向对象(OOP)编程入门教程】22.把对象串行化serialize()方法,__sleep()方法,__wakeup()方法

    有时候需要把一个对象在网络上传输,为了方便传输,可以把整个对象转化为二进制串,等到达另一端时,再还原为原来的对象,这个过程称之为串行化(也叫序列化), 就像我们现在想把一辆汽车通过轮船运到美国去,因为 ...

  4. redis--key1

    package com.ztest.redis; import java.util.Set; import com.sun.istack.internal.logging.Logger; import ...

  5. Java多线程基础知识(四)

    一. Condition 接口 1. Condition接口也提供了类似Object的监视器方法,与Lock配合可以实现等待/通知模式. 但是这两者在使用方式以及功能特性上还是有差别的. 2. 支持多 ...

  6. 谷歌黑科技WaveNet,更先进的语音合成

    导读 Google 的 DeepMind 研究实验室昨天公布了其在计算机语音合成领域的最新成果——WaveNet.该语音合成系统能够模仿人类的声音,生成的原始音频质量优于目前的文本转语音系统(text ...

  7. 在framework中打包xib

    废话不多说,直接上图 1.Copy Bundle Resources 中加入相关xib 2.这里是重点,调用的时候不能直接写 [[NSBundle mainBundle] loadNibNamed:@ ...

  8. Interleaving String leetcode

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  9. Struts2中关于"There is no Action mapped for namespace / and action name"的总结

    今天在调试一个基础的Struts2框架小程序.总是提示"There is no Action mapped for namespace / and action name"的错误. ...

  10. centos7 python3.5 下安装paramiko

    centos7 python3.5 下安装paramiko 安装开发包 yum install openssl openssl-devel python-dev -y 安装pip前需要前置安装setu ...