HTTP網路請求圖片
阿新 • • 發佈:2018-12-18
package com.example.day01_http_image01; import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainActivity extends AppCompatActivity { private EditText editText; private Button button; private ImageView imageView; private int timeout; private final int UPDATE_UI = 1; @SuppressLint("HandlerLeak") private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case UPDATE_UI: imageView.setImageBitmap((Bitmap) msg.obj); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取資源id editText = findViewById(R.id.ed_image); button = findViewById(R.id.btn_image); imageView = findViewById(R.id.image); //點選 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判斷是否有可用網路 if(!NetUtil.hasNetwork(MainActivity.this)){ Toast.makeText(MainActivity.this,"當前網路不可用",Toast.LENGTH_LONG).show(); return; } new Thread(new Runnable() { @Override public void run() { requestImage(editText.getText().toString()); } }).start(); } }); } private void requestImage(String image) { try { //定義URL地址 URL url = new URL(image); //開啟連線 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //設定請求 urlConnection.setRequestMethod("GET"); //判斷網路型別 boolean phoneNetWork = NetUtil.isPhoneNetWork(MainActivity.this); if(phoneNetWork){ timeout = 10000; }else{ timeout = 5000; } //設定超時 urlConnection.setReadTimeout(timeout); urlConnection.setConnectTimeout(timeout); //獲取請求狀態 int responseCode = urlConnection.getResponseCode(); if(responseCode == 200){ Bitmap bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream()); handler.sendMessage(handler.obtainMessage(UPDATE_UI,bitmap)); } } catch (Exception e) { e.printStackTrace(); } } }
package com.example.day01_http_image01; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class NetUtil { //是否有可用網路 public static boolean hasNetwork(Context context){ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isAvailable(); } //判斷是否是手機網路 public static boolean isPhoneNetWork(Context context){ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo(); return activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE; } }