概述
我敲好几遍啦才能一分钟完成
代码思路还是比较清晰简单的,不做过多的解释
package fengxing.primer.okhttpclient;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 1.使用之前记得添加网络权限
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* <uses-permission android:name="android.permission.INTERNET"></uses-permission>
* 2.记得添加依赖: okclient框架
* implementation 'com.squareup.okhttp3:okhttp:3.2.0'
* 3.当然,调试要开启网络数据
*/
public class MainActivity extends AppCompatActivity {
private Button button;
private ImageView image;
private OkHttpClient client;
private final String URL = "https://pic.liesio.com/2019/10/14/4403745f835d8.jpg";
private static final int GET_IMAGE_BYTE = 1;
private static final int NETWORK_ERRO = 2;
private Toast toast;
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
dealMessage(msg);
}
};
private void dealMessage(@NonNull Message msg) {
switch (msg.what){
case GET_IMAGE_BYTE:
{
byte[] imageBytes = (byte[]) msg.obj;
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
image.setImageBitmap(bitmap);
}
break;
case NETWORK_ERRO:
{
toast.makeText(MainActivity.this,"请检查网络",Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
initView();
}
private void initView() {
button = findViewById(R.id.button);
image = findViewById(R.id.image);
initViewClick();
}
private void initViewClick() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Utils.internetIsOK(MainActivity.this)) {
requstImage();
}else{
Message message = new Message();
message.what = NETWORK_ERRO;
handler.sendMessage(message);
}
}
});
}
private void requstImage() {
client = new OkHttpClient();
Request request = new Request.Builder().get().url(URL).build();
//这个请求方法是直接返回字节数组的,还有一个返回字符串的指向方法有点不一样
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
new Throwable();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
Message message = new Message();
message.what = GET_IMAGE_BYTE;
message.obj = response.body().bytes();
handler.sendMessage(message);
}else{
new Throwable();
}
}
});
}
}
最后
以上就是彩色导师为你收集整理的一分钟OkHttpClient编写请求网络图片的全部内容,希望文章能够帮你解决一分钟OkHttpClient编写请求网络图片所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复