package com.ccl.getp_w_picpath; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_path; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void getImage(View view){ String path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "地址不能為空", 0).show(); return; } try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); System.out.println("響應碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務器返回的流數(shù)據(jù) InputStream in = conn.getInputStream(); //將返回的流數(shù)據(jù)解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //顯示圖片 iv.setImageBitmap(bitmap); in.close(); } } catch (Exception e) { e.printStackTrace(); } } }
Android模擬器版本4.1.2
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比北海網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式北海網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋北海地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。
異常解釋:在主線程中的網(wǎng)絡異常
原因:Android2.3版本后不允許在主線程中直接請求網(wǎng)絡獲取數(shù)據(jù)
解決方法(兩種):
一:onCreate方法中加入如下代碼(不推薦使用,有可能阻塞Android主線程)
if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); }
二:使用Android 的handler機制,另外開啟一個子線程請求網(wǎng)絡獲取數(shù)據(jù)(推薦使用)
package com.ccl.getp_w_picpath; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_path; private ImageView iv; private String path; //使用Handler更新主線程(UI線程) private Handler handler = new Handler(){ public void handleMessage(Message msg) { Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void getImage(View view){ path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "地址不能為空", 0).show(); return; } new Thread(){ public void run(){ try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); System.out.println("響應碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務器返回的流數(shù)據(jù) InputStream in = conn.getInputStream(); //將返回的流數(shù)據(jù)解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //使用handler傳遞消息 Message msg = Message.obtain(); msg.obj = bitmap;//傳遞的數(shù)據(jù) handler.sendMessage(msg); in.close(); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } }
注意兩點:
Message對象的創(chuàng)建使用obtain方法可以達到對象重用的目的,節(jié)省內存開銷。
Message對象可以使用msg.what = 傳遞消息的類型,handler可以根據(jù)傳遞的消息類型做不同處理,優(yōu)化代碼如下:
package com.ccl.getp_w_picpath; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { protected static final int SUCCESS = 0; protected static final int ERROR = 1; protected static final int NETWORK_ERROR = 2; private EditText et_path; private ImageView iv; private String path; //使用Handler更新主線程(UI線程) private Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case SUCCESS: Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); break; case ERROR: Toast.makeText(MainActivity.this, "獲取圖片失敗", 0).show(); break; case NETWORK_ERROR: Toast.makeText(MainActivity.this, "連接網(wǎng)絡失敗", 0).show(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void getImage(View view){ path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "地址不能為空", 0).show(); return; } new Thread(){ public void run(){ try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); System.out.println("響應碼是--"+conn.getResponseCode()); if(conn.getResponseCode()==200){ //獲取服務器返回的流數(shù)據(jù) InputStream in = conn.getInputStream(); //將返回的流數(shù)據(jù)解析成圖片 Bitmap bitmap = BitmapFactory.decodeStream(in); //使用handler傳遞消息 Message msg = Message.obtain(); msg.obj = bitmap;//傳遞的數(shù)據(jù) msg.what = SUCCESS;//傳遞的消息類型,handler可根據(jù)消息類型做不同處理 handler.sendMessage(msg); in.close(); }else{ Message msg = Message.obtain(); msg.what = ERROR; handler.sendMessage(msg); } } catch (Exception e) { Message msg = Message.obtain(); msg.what = NETWORK_ERROR; handler.sendMessage(msg); e.printStackTrace(); } } }.start(); } }
分享題目:Android連接網(wǎng)絡異常:android.os.NetworkOnMainThreadException
分享地址:http://jinyejixie.com/article24/jpcoce.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、服務器托管、品牌網(wǎng)站制作、定制開發(fā)、網(wǎng)站營銷、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)