Android 네트워크 이미지 뷰 어 및 웹 소스 뷰 어
<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/1bp6EwyFhttp://pan.baidu.com/s/1c2H1JlI
이상 은 본 고의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 도움 이 되 기 를 바 랍 니 다.또한 저 희 를 많이 지지 해 주시 기 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.