使用okhttp进行图片下载,上传,get,post网络请求

github源码地址:

   https://github.com/AndroidDerek/IMoocDict

效果:

1连接网络:

使用okhttp进行图片下载,上传,get,post网络请求_第1张图片

2断开网络

使用okhttp进行图片下载,上传,get,post网络请求_第2张图片
依赖:

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

权限:

android:name="android.permission.INTERNET">

代码:

1.okhttp网络工具类——OkManager

package com.example.imoocdict;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created  on 17/4/11.
 * gradle添加依赖  compile 'com.squareup.okhttp3:okhttp:3.6.0'
 */

public class OkManager {
    private OkHttpClient client;

    //private final String TAG = OkManager.class.getSimpleName();//获得类名
    private final String TAG = "GsonUtils";


    private OkManager() {
        client = new OkHttpClient();
        //设置连接超时时间为10s
        client.newBuilder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS);
    }

    //private volatile static OkManager okManager;
    //采用单例模式获取对象
   /* public static OkManager getInstance() {
        OkManager instance = null;
        if (null == okManager) {
            //同步锁
            synchronized (OkManager.class) {
                if (null == instance) {
                    instance = new OkManager();
                    okManager = instance;
                }
            }
        }
        return instance;
    }*/


    private static OkManager okManager;
    //创建 单例模式(OkHttp官方建议如此操作)
    public static OkManager getInstance() {
        if (null == okManager) {
            synchronized (OkManager.class) {
                okManager = new OkManager();
            }
        }
        return okManager;
    }

    /**
     * 同步请求,在android开发中不常用,因为会阻塞UI线程
     *
     * @param url
     * @return
     */
    public String syncGetByURL(String url) {
        Request request = new Request.Builder().url(url).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            if (null != response && response.isSuccessful()) {
                return response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, e.toString());
        }
        return null;
    }


    /**
     * 异步get请求方式获取bitmap,通过接口回调获取
     *
     * @param url
     */
    public void getBitmap(String url) {
        final Request request = new Request.Builder().get().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure_IOException" + e.toString());
                if (null != okManagerBitmapListner) {
                    okManagerBitmapListner.onResponseFailure(call, e);
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, "onResponse=");
                if (response != null && response.isSuccessful()) {
                    byte[] bytes = response.body().bytes();
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    //方法二,可以获取字节流,然后转换成图片
                    //InputStream inputStream = response.body().byteStream();
                    //Bitmap bitmap1 = BitmapFactory.decodeStream(inputStream);
                    if (null != okManagerBitmapListner) {
                        okManagerBitmapListner.onResponseBitmap(bitmap);
                    }
                }
            }
        });
    }

    /**
     * 异步get请求获取String类型的json数据
     *
     * @param url
     * @return
     */
    public void getString(String url) {

        final Request request = new Request.Builder().url(url).get().build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "IOException" + e.toString());
                if (null != okManagerStringListner) {
                    okManagerStringListner.onResponseFailure(call, e);
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (null != response && response.isSuccessful()) {
                    ResponseBody body = response.body();
                    String string = body.string();
                    Log.d(TAG, "string=" + string);
                    if (null != okManagerStringListner) {
                        okManagerStringListner.onResponseString(string);
                    }
                }
            }
        });

    }

    /**
     * post表单请求提交,获取json字符串
     *
     * @param url
     * @param params
     */
    public void postComplexForm(String url, Map, String> params) {

        FormBody.Builder builder = new FormBody.Builder();//表单对象,包含以input开始的对象,以html表单为主
        //map集合中的参数添加到FormBody表单中.
        if (null != params && !params.isEmpty()) {
            for (Map.Entry, String> entry : params.entrySet()) {
                builder.add(entry.getKey(), entry.getValue());
            }
            RequestBody requestBody = builder.build();//创建请求体
            Request request = new Request.Builder().url(url).post(requestBody).build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.d(TAG, "IOException" + e.toString());
                    if (null != okManagerStringListner) {
                        okManagerStringListner.onResponseFailure(call, e);
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (null != response && response.isSuccessful()) {
                        String string = response.body().string();
                        Log.d(TAG, "string=" + string);
                        if (null != okManagerStringListner) {
                            okManagerStringListner.onResponseString(string);
                        }
                    }
                }
            });

        }
    }

    /**
     * Bitmap的接口
     */
    interface OkManagerBitmapListner {
        void onResponseBitmap(Bitmap bitmap);

        void onResponseFailure(Call call, IOException e);
    }

    public OkManagerBitmapListner getOkManagerBitmapListner() {
        return okManagerBitmapListner;
    }

    public void setOkManagerBitmapListner(OkManagerBitmapListner okManagerListner) {
        this.okManagerBitmapListner = okManagerListner;
    }

    private OkManagerBitmapListner okManagerBitmapListner;


    /**
     * String的接口
     */
    interface OkManagerStringListner {
        void onResponseString(String string);

        void onResponseFailure(Call call, IOException e);
    }

    private OkManagerStringListner okManagerStringListner;

    public OkManagerStringListner getOkManagerStringListner() {
        return okManagerStringListner;
    }

    public void setOkManagerStringListner(OkManagerStringListner okManagerStringListner) {
        this.okManagerStringListner = okManagerStringListner;
    }


}

2.主代码

package com.example.imoocdict;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 使用okhttp进行图片下载,上传,get,post网络请求
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "GsonUtils";
    private static final int SUCCESS_STATUS = 1;
    private static final int ERROR_STATUS = 0;
    private TextView voice;
    private EditText input;
    private Button find;
    private LinearLayout activity_main;
    private String findWords;
    private String string;
    private Button syncgetbyurl;
    private Button syncgetdrawableurl;
    private ImageView imageView;
    private OkManager okManager;
    private TextView translate;
    private Button upload;
    private Looper mainLooper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mainLooper = Looper.getMainLooper();
        //okManager = OkManager.getInstance();
    }

    private void initView() {
        voice = (TextView) findViewById(R.id.voice);
        input = (EditText) findViewById(R.id.input);
        find = (Button) findViewById(R.id.find);
        activity_main = (LinearLayout) findViewById(R.id.activity_main);

        find.setOnClickListener(this);
        syncgetbyurl = (Button) findViewById(R.id.syncgetbyurl);
        syncgetbyurl.setOnClickListener(this);
        syncgetdrawableurl = (Button) findViewById(R.id.syncgetdrawableurl);
        syncgetdrawableurl.setOnClickListener(this);
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setOnClickListener(this);
        translate = (TextView) findViewById(R.id.translate);
        translate.setOnClickListener(this);
        upload = (Button) findViewById(R.id.upload);
        upload.setOnClickListener(this);
    }

    String rootPath = "http://fanyi.youdao.com/openapi.do";
    /**
     * 有道词典提供的api查询接口,q=后面拼接的是需要查询的单词
     * 参考网站
     * 例如:查询words,对应的http://fanyi.youdao.com/openapi.do?keyfrom=imoocdict123456&key=324273592
     * &type=data&doctype=json&version=1.1&q=words
     */
    String path = "http://fanyi.youdao.com/openapi.do?keyfrom=imoocdict123456&key=324273592&" +
            "type=data&doctype=json&version=1.1&q=";


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //图片上传成功的标识
            if (msg.what == SUCCESS_STATUS) {
                Log.d(TAG, "SUCCESS_STATUS=");
                findString = "图片上传成功,返回的json:" + (String) msg.obj;
                translate.setText(findString);
            }
            if (msg.what == ERROR_STATUS) {
                Log.d(TAG, "ERROR_STATUS=");
                translate.setText("网络异常,服务器繁忙");
                //Toast.makeText(MainActivity.this, "网络异常,服务器繁忙", Toast.LENGTH_SHORT).show();
            }
        }
    };
    String findString;

    @Override
    public void onClick(View v) {
        submit();
        switch (v.getId()) {
            case R.id.find:
                final String findUrl = path + findWords;
                if (null == okManager) {
                    okManager = OkManager.getInstance();
                }
                Log.d(TAG, "findUrl=" + findUrl);
                Log.d(TAG, "okManager=" + okManager);
                okManager.setOkManagerStringListner(new OkManager.OkManagerStringListner() {
                    @Override
                    public void onResponseString(String string) {
                        findString = string;
                        Log.d(TAG, "findString=" + findString);
                        //ui主线程中更新ui
                       /* handler.post(new Runnable() {
                            @Override
                            public void run() {
                                translate.setText(findString);
                            }
                        });*/
                        Message message = Message.obtain();
                        message.what = SUCCESS_STATUS;
                        message.obj = string;
                        handler.sendMessage(message);
                    }

                    @Override
                    public void onResponseFailure(Call call, IOException e) {
                        //Toast需要在ui主线程中Looper中执行
                        //                        mainLooper.prepare();
                        //                        new Handler().post(new Runnable() {
                        //                            @Override
                        //                            public void run() {
                        //                                Toast.makeText(MainActivity.this,
                        // "网络异常,服务器繁忙", Toast.LENGTH_SHORT).show();
                        //                            }
                        //                        });
                        //
                        //                        mainLooper.loop();
                        //错误:Can't create handler inside thread that has not called Looper.prepare()

                       /* MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "网络异常,服务器繁忙", Toast.LENGTH_SHORT).show();
                            }
                        });*/
                        Message message = Message.obtain();
                        message.what = ERROR_STATUS;
                        handler.sendMessage(message);
                    }
                });
                //进行get请求
                okManager.getString(findUrl);

                break;
            case R.id.syncgetbyurl:
                if (null == okManager) {
                    okManager = OkManager.getInstance();
                }
                okManager.setOkManagerStringListner(new OkManager.OkManagerStringListner() {
                    @Override
                    public void onResponseString(String string) {
                        findString = string;
                        Log.d(TAG, "findString=" + findString);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                translate.setText(findString);
                            }
                        });
                    }

                    @Override
                    public void onResponseFailure(Call call, IOException e) {

                    }
                });
                Map, String> params = new HashMap<>();
                params.put("keyfrom", "imoocdict123456");
                params.put("key", "324273592");
                params.put("type", "data");
                params.put("doctype", "json");
                params.put("version", "1.1");
                params.put("q", findWords);
                //进行post请求
                okManager.postComplexForm(rootPath, params);
                break;
            case R.id.syncgetdrawableurl:
                if (null == okManager) {
                    okManager = OkManager.getInstance();
                }
                String imageUrl = "https://www.baidu.com/img/bd_logo1.png";
                okManager.setOkManagerBitmapListner(new OkManager.OkManagerBitmapListner() {
                    @Override
                    public void onResponseBitmap(Bitmap bitmap) {
                        Log.d(TAG, "imageBitmap");
                        imageBitmap = bitmap;
                        //在主线程中更新ui
                        MainActivity.this.runOnUiThread(imageRunnable);
                    }

                    @Override
                    public void onResponseFailure(Call call, IOException e) {
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "网络异常,服务器繁忙",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
                //加载网络图片
                okManager.getBitmap(imageUrl);
                break;
            case R.id.upload:
                if (null == okManager) {
                    okManager = OkManager.getInstance();
                }
                //从资源中获取的Drawable --> Bitmap
                Resources resources = getResources();
                Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.login_bg);
                /***开发中使用bitmap转换成base64格式的字符串,提交给服务器***/
                //String base64 = "/9j/4AAQSkZJRgABAQAAAQABAAD";
                String base64 = bitmapToBase64(bitmap);
                Log.d(TAG, "base64=" + base64);
                // 这是本地服务器的上传测试地址
                String uploadUrl = "http://192.168.2.241/upark/upload";
                Map, String> loadParams = new HashMap<>();
                loadParams.put("id", "-1");
                loadParams.put("base64", base64);
                loadParams.put("type", "png");
                loadParams.put("model", "groupService");
                loadParams.put("serverCode", "other");
                okManager.setOkManagerStringListner(new OkManager.OkManagerStringListner() {
                    @Override
                    public void onResponseString(String string) {
                        //图片上传的成功回调
                        findString = string;
                        Log.d(TAG, "findString=" + findString);
                        Message message = Message.obtain();
                        message.what = SUCCESS_STATUS;
                        message.obj = findString;
                        handler.sendMessage(message);
                    }

                    @Override
                    public void onResponseFailure(Call call, IOException e) {
                        //Toast需要在ui主线程中Looper中执行
                        mainLooper.prepare();
                        Toast.makeText(MainActivity.this, "网络异常,服务器繁忙",
                                Toast.LENGTH_SHORT).show();
                        mainLooper.loop();
                    }
                });
                //进行图片的上传
                okManager.postComplexForm(uploadUrl, loadParams);
                break;
        }
    }

    private Bitmap imageBitmap;
    //展示加载的图片
    Runnable imageRunnable = new Runnable() {
        @Override
        public void run() {
            imageView.setImageBitmap(imageBitmap);
        }
    };

    private void submit() {
        // validate
        String inputString = input.getText().toString().trim();
        if (TextUtils.isEmpty(inputString)) {
            Toast.makeText(this, "inputString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        findWords = inputString;
        // TODO validate success, do something
    }

    /**
     * bitmap转为base64
     *
     * @param bitmap
     * @return
     */
    public String bitmapToBase64(Bitmap bitmap) {
        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}


布局:

xml version="1.0" encoding="utf-8"?>
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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.imoocdict.MainActivity">
    android:layout_width="match_parent"
                android:layout_height="200dp">
        <TextView
            android:id="@+id/translate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    
    android:layout_width="match_parent"
                  android:layout_height="50dp"
                  android:orientation="horizontal">

        <TextView
            android:id="@+id/voice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="语音"
        />
                    android:id="@+id/input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="words"/>
        

。。。

你可能感兴趣的:(安卓学习笔记)