关闭

简单实用的OkHttp3入门精简教程

标签: Okhttp拦截器getpost上传
1330人阅读 评论(1) 收藏 举报
分类:

自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理


探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制


Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南


本文将用最简的代码和语句介绍OkHttp3的常用基本操作。

准备工作

导入OkHttp3

 compile 'com.squareup.okhttp3:okhttp:3.8.1'

添加权限


    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

请注意:假若你看到此处不明白为什么需要做这些准备工作,那么请点击浏览器右上角的八叉 ( X )

Post,Get,上传,下载

package com.cn.okhttp01;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
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.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 原创作者:谷哥的小弟
 * 博客地址:http://blog.csdn.net/lfdfhl
 */
public class MainActivity extends AppCompatActivity {
    public final static String TAG="reborn";
    private static final int REQUEST_CODE = 9527;
    private static String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init(){
        mContext=this;
        verifyStoragePermissions(this);
    }

    private void testOkHttp(){
        testUpload();
    }

    public void verifyStoragePermissions(Activity activity) {
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, PERMISSIONS, REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    testOkHttp();
                } else {
                    Toast.makeText(mContext, "您未授权存储权限", Toast.LENGTH_LONG).show();
                }
                break;
            default:
        }
    }


    //Get同步请求
    public void testGet1(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url="http://blog.csdn.net/lfdfhl";
                    OkHttpClient okHttpClient=new OkHttpClient();
                    Request.Builder requestBuilder=new Request.Builder();
                    requestBuilder.url(url);
                    Request request=requestBuilder.build();
                    Call call=okHttpClient.newCall(request);
                    Response response=call.execute();
                    String result=response.body().string();
                    response.body().close();
                    Log.i(TAG,"测试Get同步请求 "+result);
                }catch (Exception e){

                }
            }
        }).start();
    }

    //Get异步请求
    public void testGet2(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url="http://blog.csdn.net/lfdfhl";
                    OkHttpClient okHttpClient=new OkHttpClient();
                    Request.Builder requestBuilder=new Request.Builder();
                    requestBuilder.url(url);
                    Request request=requestBuilder.build();
                    Call call=okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            int code=response.code();
                            String result=response.body().string();
                            response.body().close();
                            Log.i(TAG,"code="+code);
                            Log.i(TAG,"测试Get异步请求 "+result);
                        }
                    });
                }catch (Exception e){

                }
            }
        }).start();
    }


    //POST同步请求
    public void testPost1(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url="http://106.14.136.52:8080/user/login";
                    OkHttpClient okHttpClient=new OkHttpClient();
                    Request.Builder requestBuilder=new Request.Builder();
                    FormBody.Builder formBodyBuilder=new FormBody.Builder();
                    formBodyBuilder.add("username","andy");
                    formBodyBuilder.add("password","123456");
                    RequestBody requestBody=formBodyBuilder.build();
                    requestBuilder.url(url);
                    requestBuilder.post(requestBody);
                    requestBuilder.addHeader("apikey","81bf9da930c7f9825a3c3383f1d8d766");
                    Request request=requestBuilder.build();
                    Response response=okHttpClient.newCall(request).execute();
                    int code=response.code();
                    String result=response.body().string();
                    response.body().close();
                    Log.i(TAG,"code="+code);
                    Log.i(TAG,"测试Post同步请求 "+result);
                }catch (Exception e){

                }
            }
        }).start();
    }


    //POST异步请求
    public void testPost2(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url="http://106.14.136.52:8080/user/login";
                    OkHttpClient okHttpClient=new OkHttpClient();
                    Request.Builder requestBuilder=new Request.Builder();
                    FormBody.Builder formBodyBuilder=new FormBody.Builder();
                    formBodyBuilder.add("username","andy");
                    formBodyBuilder.add("password","123456");
                    RequestBody requestBody=formBodyBuilder.build();
                    requestBuilder.url(url);
                    requestBuilder.post(requestBody);
                    Request request=requestBuilder.build();
                    Call call=okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            int code=response.code();
                            String result=response.body().string();
                            response.body().close();
                            Log.i(TAG,"code="+code);
                            Log.i(TAG,"测试Post异步请求 "+result);
                        }
                    });

                }catch (Exception e){

                }
            }
        }).start();
    }

    //下载
    public void testDownload(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url="http://img.blog.csdn.net/20161023140032428";
                    final File downloadFile=new File(getExternalCacheDir().toString()+File.separator+"test.jpg");
                    OkHttpClient okHttpClient=new OkHttpClient();
                    Request.Builder requestBuilder=new Request.Builder();
                    requestBuilder.url(url);
                    Request request=requestBuilder.build();
                    Call call=okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if(response.isSuccessful()){
                                InputStream inputStream=response.body().byteStream();
                                FileOutputStream fileOutputStream=new FileOutputStream(downloadFile);
                                int len=0;
                                byte b []=new byte[1024*2];
                                while ((len=inputStream.read(b))!=-1){
                                    fileOutputStream.write(b,0,len);
                                }
                                fileOutputStream.close();
                                inputStream.close();
                            }
                            response.body().close();
                            Bitmap bitmap = BitmapFactory.decodeFile(downloadFile.getAbsolutePath());
                            Log.i(TAG,"width="+bitmap.getWidth());
                            Log.i(TAG,"height="+bitmap.getHeight());
                        }
                    });

                }catch (Exception e){

                }
            }
        }).start();
    }

    //上传
    public void testUpload(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url ="http:192.168.0.103:8081/SpringMVC07/testUpload/uploadFile.do";
                    OkHttpClient okHttpClient=new OkHttpClient();
                    MultipartBody.Builder multipartBodyBuilder=new MultipartBody.Builder();
                    //添加文件
                    File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"test.jpg");
                    String fileName=file.getName();
                    MediaType mediaType=MediaType.parse("image/*");
                    RequestBody requestBody=RequestBody.create(mediaType,file);
                    multipartBodyBuilder.setType(MultipartBody.FORM);
                    multipartBodyBuilder.addFormDataPart("photo",fileName,requestBody);
                    //添加参数
                    multipartBodyBuilder.addFormDataPart("name","zxx");
                    multipartBodyBuilder.addFormDataPart("number","9527");
                    multipartBodyBuilder.addFormDataPart("country","China");
                    MultipartBody multipartBody=multipartBodyBuilder.build();
                    Request.Builder requestBuilder=new Request.Builder();
                    requestBuilder.url(url);
                    requestBuilder.post(multipartBody);
                    Request request=requestBuilder.build();
                    Call call=okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.i(TAG,"onFailure");
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            Log.i(TAG,"onResponse");
                        }
                    });

                }catch (Exception e){

                }
            }
        }).start();
    }

}

请注意:测试时,请将url替换成您自己的可用地址

OkHttp拦截器

package com.cn.okhttp02;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 原创作者:谷哥的小弟
 * 博客地址:http://blog.csdn.net/lfdfhl
 */
public class MainActivity extends AppCompatActivity {
    public final static String TAG="reborn";
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        testApplicationInterceptors();
        testNetworkInterceptors();
    }
    private void init(){
        mContext=this;
    }


    //测试Application Interceptors
    public void testApplicationInterceptors(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Log.i(TAG,"----> run");
                    String url="http://blog.csdn.net/lfdfhl";
                    OkHttpClient.Builder okHttpClientBuilder= new OkHttpClient.Builder();
                    //添加应用拦截器
                    okHttpClientBuilder.addInterceptor(new LoggingInterceptor());
                    OkHttpClient okHttpClient=okHttpClientBuilder.build();
                    Request.Builder requestBuilder=new Request.Builder();
                    requestBuilder.url(url);
                    requestBuilder.addHeader("os","Android");
                    requestBuilder.addHeader("country","China");
                    Request request=requestBuilder.build();
                    Call call=okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.i(TAG,"----> onFailure");
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            Log.i(TAG,"----> onResponse");
                            int code=response.code();
                            String result=response.body().string();
                            response.body().close();
                            Log.i(TAG,"测试Application Interceptors "+result);
                        }
                    });
                }catch (Exception e){

                }
            }
        }).start();
    }


    //测试Network Interceptors
    public void testNetworkInterceptors(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Log.i(TAG,"----> run");
                    String url="http:www.github.com";
                    OkHttpClient.Builder okHttpClientBuilder= new OkHttpClient.Builder();
                    //添加网络拦截器
                    okHttpClientBuilder.addNetworkInterceptor(new LoggingInterceptor());
                    OkHttpClient okHttpClient=okHttpClientBuilder.build();
                    Request.Builder requestBuilder=new Request.Builder();
                    requestBuilder.url(url);
                    requestBuilder.addHeader("os","Android");
                    requestBuilder.addHeader("country","China");
                    Request request=requestBuilder.build();
                    Call call=okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.i(TAG,"----> onFailure");
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            Log.i(TAG,"----> onResponse");
                            int code=response.code();
                            String result=response.body().string();
                            response.body().close();
                            Log.i(TAG,"测试Network Interceptors "+result);
                        }
                    });
                }catch (Exception e){

                }
            }
        }).start();
    }


    class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Log.i(TAG,"----> intercept");
            Request request = chain.request();
            long t1 = System.nanoTime();
            Log.i(TAG, "intercept: " + String.format("Sending request %s on %s%n%s",request.url(), chain.connection(), request.headers()));
            Response response = chain.proceed(request);
            long t2 = System.nanoTime();
            Log.i(TAG, "intercept: " + String.format("Received response for %s in %.1fms%n%s",response.request().url(), (t2 - t1) / 1e6d, response.headers()));
            return response;
        }
    }

}

什么是拦截器呢?

拦截器能对Call进行监测、改写、重试连接;它能够对请求和响应进行二次加工。通俗地说:拦截器是请求和回复之间的一道门——发出请求时数据需要从这道门出去;接收响应时数据需要从这道门进来。当然,当数据(不论进出)经过这道门时,该门可对数据进行某些操作。

OkHttp3常用的拦截器分为:

  • Application Interceptors 应用拦截器
  • Network Interceptors 网络拦截器

这两个拦截器有什么区别呢?

Application interceptors 应用拦截器

  • Don’t need to worry about intermediate responses like redirects and retries.
    不关心中间过程的响应,如重定向和重试

  • Are always invoked once, even if the HTTP response is served from the cache.
    总是只调用一次,即使HTTP响应是从缓存中获取

  • Observe the application’s original intent. Unconcerned with OkHttp-injected headers like If-None-Match.
    关注应用程序的初衷. 不关心OkHttp注入的头信息如: If-None-Match.

  • Permitted to short-circuit and not call Chain.proceed().
    允许短路且不调用Chain.proceed()

  • Permitted to retry and make multiple calls to Chain.proceed().
    允许重试,多次调用 Chain.proceed()

Network Interceptors 网络拦截器

  • Able to operate on intermediate responses like redirects and retries.
    能够操作中间过程的响应,如重定向和重试.

  • Not invoked for cached responses that short-circuit the network.
    当网络短路返回缓存响应时不被调用

  • Observe the data just as it will be transmitted over the network.
    只观察在网络上传输的数据.

  • Access to the Connection that carries the request.
    携带请求访问连接

拦截器使用场景

  • 为request统一添加header
  • 检查response判定用户登录信息是否失效,并用Dialog提示用户登录
  • 重写响应头和改变响应体。但不是很推荐这么做,因为可能违反web服务器的期望。
2
0
查看评论

最简单的C#快速入门教程

在一小时内学会 C#。使用例程,简单却完整的探索 C# 语言的构造和特点。本文特别适合有 C++ 基础却没有太多精力学习 C# 的读者。 简介         C# 是一种具有 C++ 特性,Java 样式及 BASIC ...
  • phenixyf
  • phenixyf
  • 2013-05-22 17:51
  • 1915

最简单的Dagger2入门教程

在上一篇博客中总结了依赖注入的原理与实现方式。 依赖注入的原理 依赖注入就是将调用者需要的另一个对象实例不在调用者内部实现,而是通过一定的方式从外部传入实例,解决了各个类之间的耦合。那么这个外部,到底指的是哪里,如果指的是另一个类,那么,另一个类内部不就耦合了。能不能有一种方式,将这些构造的对象放到...
  • lisdye2
  • lisdye2
  • 2016-07-18 15:38
  • 17816

最简单的MFC入门教程---WinMain函数基础篇

最简单的MFC入门教程您可以随便复制本教程,但是请务必保证本教程的完整性,如果您发现有任何技术方面的漏洞请和xiaobo68688@qq.com联系,或者只是想和我交个朋友也OK!Kunsa拜上!~~~~这几天萌生了一些写一点东西的想法,以来把自己的知识和大家共享一下,二来是对自己知识的一个总结。就...
  • xiaobo68688
  • xiaobo68688
  • 2010-05-24 14:30
  • 6386

qt入门,最简单的qt程序

    最近因为工作的关系需要研究qt了,客观的说,作为一个跨平台的代码框架,qt还是不错的,网上有大量吹捧qt的文章,我就不重复了。但是qt 也实在并不可爱,可能是因为人手太少的原因,qt的相关书籍,示例代码,帮助系统,辅助工具,社区支持等等做得都远远不够。当然我这样...
  • oowgsoo
  • oowgsoo
  • 2006-12-16 14:45
  • 12647

对OKHttp3的简单封装

一个完整的Android项目,肯定少不了网络请求的相关类。我整理了一下自己现在项目里二次封装的网络请求类,方便以后查阅,也希望大家指正。Http请求使用了现在主流的第三方库OKHttp3,数据是json格式的,我直接用了gson。添加对应的依赖: compile 'com.squareup....
  • qq_33724789
  • qq_33724789
  • 2017-07-03 17:40
  • 619

Retrofit2+okhttp3 使用教程

Retrofit介绍: Retrofit(GitHub主页https://github.com/square/okhttp)和OkHttp师出同门,也是Square的开源库,它是一个类型安全的网络请求库,Retrofit简化了网络请求流程,基于OkHtttp做了封装,解耦的更彻底:比方说通过注解来...
  • hanj456
  • hanj456
  • 2016-12-09 15:37
  • 1258

android之 eventBus简单实用

一个eventbus的简单实用
  • csdn_loveQingQing
  • csdn_loveQingQing
  • 2017-07-28 10:04
  • 140

最简洁最没有废话的python入门教程

斗鱼直播间 584723555670668 QQ群 python入门教程视频下载地址博客地址入门 python 变量 视频地址 python 运算符 视频地址 python 语句 视频地址 python list、tuple、dict、set 视频地址 python 函数初窥 ...
  • cugb1004101218
  • cugb1004101218
  • 2016-07-16 23:23
  • 956

pyCharm简单的使用

简介 pyCharm 是一个 python 的 IDE ,本文只做一个简单的创建一个项目和调试使用的部分,why ? python是啥 本文不是你。。可以离开了    下载      https://www.jetbrains.com/...
  • w739639550
  • w739639550
  • 2017-07-16 15:47
  • 204

实用技巧!从未见过如此简单粗暴的Hibernate教程

贺必贇美食 2017-02-18 06:25 内容大纲.png 引题 对象关系映射(Object Relational Mapping,简称ORM)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的规范,简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将jav...
  • u011277123
  • u011277123
  • 2017-02-28 10:25
  • 1807
    个人资料
    • 访问:1943530次
    • 积分:27792
    • 等级:
    • 排名:第234名
    • 原创:819篇
    • 转载:0篇
    • 译文:1篇
    • 评论:1296条
    博客专栏
    开发交流


    为方便大家学习和交流Android开发,建了个群,欢迎大家加入。

    QQ群: 183899857(已满)
    QQ群: 250468947(新开)

    文章分类