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-animations和transitions性能:浏览器到底做了什么?

    CSS animations 和 transitions 的性能:浏览器到底做了什么?(译) 原文地址:http://blogs.adobe.com/webplatform/2014/03/18/cs ...

  2. freemarker初级教程(一)

    序,freemarker是一个模板引擎 一.好处 MVC分离 易于扩展 分离可视化设计和应用程序逻辑 分离页面设计员和程序员. 处理XML和HTML都可以,可以从文本文件读取 二.

  3. 自执行的匿名函数!function()

    最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭州最后参加团队会议的时候,@西子剑影抛出的一样的问题:如果在function之前加上感叹号 (!) 会 ...

  4. NOIP2005 等价表达式

    题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数 ...

  5. uva.10020 Minimal coverage(贪心)

    10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...

  6. CentOS6.3编译安装Memcached集群分布式缓存代理Magent-0.6出错汇总

    参考文章:Memcached集群/分布式/高可用 及 Magent缓存代理搭建过程 详解,搭建Magent,在编译的过程中会出现很多错误: #编译安装安装magent到 /usr/local/mage ...

  7. Mac Pro 修改环境变量

    参考:Ubuntu 12 修改环境变量 [实战] 把 php.php-fpm.nginx.mysql 的相关命令路径添加到 用户环境变量 $ vim ~/.bash_profile alias ll= ...

  8. 比较两个数据库表table结构不同之处

    /*--比较两个数据库的表字段差异 hy 适用多种版本库 --*/ /*--调用示例 exec p_comparestructure 'database1','database2' --*/ ) dr ...

  9. 优雅地使用Windows

    优雅地使用Windows 理财推荐:收益还行,安全性比余额宝高,只能自己的卡转进转出所以被盗也不怕,重要的是快速取现实时到账呢 1 现金宝 :点击进入现金宝 或者百度现金宝 2 百度理财 8.baid ...

  10. 2015安徽省赛 I.梯田

    http://xcacm.hfut.edu.cn/problem.php?id=1213 set + 搜索 姐姐是用搜索+二分做的,效率要高很多 #include<iostream> #i ...