业无高卑志当坚,男儿有求安得闲?这篇文章主要讲述Android-okhttp?????????????????????????????????相关的知识,希望能为你提供帮助。
?????????http
puts
info
3.1
null
list
man
toast
exception
???androidManifest.xml???????????????????????????
??????< !-- ?????????????????????????????? ?????????????????? --> < uses-permission android:name="android.permission.INTERNET" /> < !-- ?????????????????????????????? ?????????????????? --> < uses-permission android:name="android.permission.SET_WALLPAPER" />
【Android-okhttp?????????????????????????????????】
??? app/build.gradle ??????
implementation ???com.squareup.okhttp3:okhttp:3.10.0???
???????????? sync now ??????okhttp?????????
文章图片
MainActivity
package liudeli.async.okhttp2; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import java.io.IOException; import java.io.InputStream; import liudeli.async.R; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends Activity {private final static String TAG = MainActivity.class.getSimpleName(); // ???????????? private final String PATH = "https://timgsa.baidu.com/timg?image& quality=80& size=b9999_10000" + "& sec=1544714792699& di=3c2de372608ed6323f583f1c1b445e51& imgtype=0& src=https://www.songbingjia.com/android/http%3A%2F%2Fp" + "2.qhimgs4.com%2Ft0105d27180a686e91f.jpg"; private ImageView imageView; private Button bt_set_wallpaper; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); imageView = findViewById(R.id.iv_image); bt_set_wallpaper = findViewById(R.id.bt_set_wallpaper); Button bt_get_image = findViewById(R.id.bt_get_image); bt_get_image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ??????????????? progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setMessage("Download ..."); progressDialog.show(); /** * ??????????????????????????? ?????? */ /*new Thread(){ @Override public void run() { super.run(); downloadImage1(); } }.start(); *//** * ??????????????????????????? ?????? */ downloadImage2(); } }); bt_set_wallpaper.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != bitmap) { try { setWallpaper(bitmap); Toast.makeText(MainActivity.this, "??????????????????", Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "??????????????????", Toast.LENGTH_LONG).show(); } } } }); }private Bitmap bitmap; /** * ??????????????? * ??????okhttp ?????????????????? */ private void downloadImage1() {OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(PATH) .build(); try { Response response = client.newCall(request).execute(); InputStream inputStream = response.body().byteStream(); if (200 == response.code()) { Bitmap bitmap = BitmapFactory.decodeStream(inputStream); showUI(bitmap); } else { showUI(null); }} catch (IOException e) { e.printStackTrace(); showUI(null); } }/** * ??????????????? * ??????okhttp ?????????????????? */ private void downloadImage2() {OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(PATH) .build(); try { Call call = client.newCall(request); // ??????client?????????call.enqueue(new Callback() { // ???????????????> > > ?????????????????????????????? @Override public void onFailure(Call call, IOException e) { showUI(null); // ?????????????????????UI }@Override public void onResponse(Call call, Response response) throws IOException { InputStream inputStream = response.body().byteStream(); if (200 == response.code()) { Bitmap bitmap = BitmapFactory.decodeStream(inputStream); showUI(bitmap); } else { showUI(null); } } }); } catch (Exception e) { e.printStackTrace(); showUI(null); } }/** * ??????UI ????????????????????? ????????? ????????? ???UI?????????????????? * @param bitmap */ private void showUI(final Bitmap bitmap) { this.bitmap = bitmap; runOnUiThread(runnable); }Runnable runnable = new Runnable() { @Override public void run() { if (bitmap != null) {// ????????????????????????????????????????????? new Handler().postDelayed(new Runnable() { @Override public void run() { // ?????????????????????????????? imageView.setImageBitmap(bitmap); // ????????????????????? bt_set_wallpaper.setEnabled(true); // ??????????????? progressDialog.dismiss(); Toast.makeText(MainActivity.this, "????????????", Toast.LENGTH_SHORT).show(); } }, 2000); } else { //?????? bt_set_wallpaper.setEnabled(false); Toast.makeText(MainActivity.this, "????????????,???????????????", Toast.LENGTH_LONG).show(); // ??????????????? progressDialog.dismiss(); } } }; }
activity_main
< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> < Button android:id="@+id/bt_get_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="????????????" android:onClick="getImage" android:layout_marginLeft="20dp" /> < Button android:id="@+id/bt_set_wallpaper" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="????????????" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:enabled="false" /> < ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/bt_get_image" /> < /RelativeLayout>
???????????????
文章图片
推荐阅读
- appium框架之bootstrap
- Android 代码开发工作流
- Android重复点击事件处理
- android开发时如何让svn更快些
- iPad微信协议抖音协议安卓协议
- Android System trace分析
- 微信域名防封技术,APP推广微信域名怎么避免防封,如何防拦截()
- 推荐一波2018年让人惊喜的手机小众APP
- create-react-app react脚手架