国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 網絡圖片查看器與網頁源碼查看器

2019-12-12 02:58:30
字體:
來源:轉載
供稿:網友

在AndroidManifest.xml里面先添加權限訪問網絡的權限:

<uses-permission android:name="android.permission.INTERNET"/>

效果圖如下:

下面是主要代碼:

package com.hb.neting;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.annotation.SuppressLint;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 ImageView iv_show; private EditText et_input; private String path; private int code; private HttpURLConnection conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_show=(ImageView) findViewById(R.id.iv_show); et_input=(EditText) findViewById(R.id.et_inpput); } @SuppressLint("ShowToast") public void chakan(View view){ path = et_input.getText().toString().trim(); if (TextUtils.isEmpty(path)) { Toast.makeText(MainActivity.this, "不能輸入空的", 0).show(); return; } new Thread(){ public void run() { try {  URL url = new URL(path);  conn = (HttpURLConnection) url.openConnection();  conn.setRequestMethod("GET");  conn.setConnectTimeout(5000);  code = conn.getResponseCode();  if(code==200){  InputStream in = conn.getInputStream();  //解析圖片  final Bitmap stream = BitmapFactory.decodeStream(in);  runOnUiThread(new Runnable() {  public void run() {  //更新UI  iv_show.setImageBitmap(stream);  }  });  in.close();  } } catch (Exception e) {  e.printStackTrace(); } }; }.start(); }}

這是xml的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_inpput" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入獲取圖片的地址:" /> <Button  android:id="@+id/bt_read" android:onClick="chakan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看" /> <ImageView android:id="@+id/iv_show" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>

源碼: http://pan.baidu.com/s/1bp6EwyF

接著看一下網頁源碼查看器的小案例:

既然都涉及到網絡的添加一個如上的網絡權限是必不可少的了,具體操做如上所示,先看效果圖:

主要代碼:

package com.hb.network;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.annotation.SuppressLint;import android.app.Activity;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.TextView;import android.widget.Toast;import com.hb.utils.ReadStreamUtils;public class MainActivity extends Activity { protected static final int SUCESS = 0; protected static final int EORR = 1; private TextView tv_show;  private EditText et_input; private URL url; private String path; @SuppressLint("HandlerLeak")  private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case SUCESS: String content=(String) msg.obj; tv_show.setText(content); break; case EORR: Toast.makeText(MainActivity.this,"查看源碼失敗" , 0).show(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_show=(TextView) findViewById(R.id.tv_show); et_input=(EditText) findViewById(R.id.et_input); } public void onclick(View view){ path = et_input.getText().toString().trim(); if(TextUtils.isEmpty(path)){ return; }new Thread(){ public void run() { try {  url = new URL(path);  //判斷從EditText獲取的數據否為空  if(TextUtils.isEmpty(path)){  return;  }  HttpURLConnection conn = (HttpURLConnection) url.openConnection();  conn.setConnectTimeout(3000);  conn.setRequestMethod("GET");  int code = conn.getResponseCode();  if(code == 200){  InputStream is= conn.getInputStream();  String content = ReadStreamUtils.Read(is);  Message msg = new Message();  msg.what=SUCESS;  msg.obj=content;  handler.sendMessage(msg);  } } catch (Exception e) {  e.printStackTrace();  Message msg = new Message();  msg.what=EORR;  handler.sendMessage(msg); } }; }.start(); }}
package com.hb.utils;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class ReadStreamUtils {/** * 讀取流的輸入 * @param is * @return * @throws IOException */ public static String Read(InputStream is) throws IOException{ ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len; byte [] buffer=new byte[1024]; while((len=is.read(buffer))!=-1){ bos.write(buffer,0,len); } is.close(); bos.close(); String temp = bos.toString(); if(temp.contains("charset=utf-8")){ return bos.toString("utf-8"); }else if(temp.contains("charset=iso-8859-1")){ return bos.toString("iso-8859-1"); } return null; }}

及xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入要查看源碼的網址:" /> <Button android:onClick="onclick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看" android:textSize="25sp" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView></LinearLayout>

源碼: http://pan.baidu.com/s/1bp6EwyF

         http://pan.baidu.com/s/1c2H1JlI

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南澳县| 通辽市| 廊坊市| 开封市| 安塞县| 濉溪县| 临沂市| 闻喜县| 蓬安县| 顺平县| 南岸区| 资阳市| 南丹县| 太湖县| 井陉县| 武乡县| 基隆市| 若羌县| 中宁县| 屯门区| 当阳市| 武平县| 富蕴县| 罗山县| 兰考县| 冕宁县| 防城港市| 库车县| 寿阳县| 肇东市| 三门峡市| 祥云县| 清镇市| 团风县| 温州市| 承德县| 新化县| 乌兰察布市| 万荣县| 栾川县| 玉树县|