android實現(xiàn)網(wǎng)頁源碼查看器。思路是,先把組件設(shè)計好,然后編寫java代碼來實現(xiàn)整個流程。主要通過實現(xiàn)網(wǎng)頁源碼查看器的過程,來理解handle的流程。
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比清徐網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式清徐網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋清徐地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。
第一部分:
以上圖片是組件的代碼。
第二部分:
public class MainActivity extends AppCompatActivity {
private EditText et_url;
private button btn_show;
private TextView tv_code;
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
String temp = (String) msg.obj;
tv_code.setText(temp);
}
};
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_url = (EditText) findViewbyId(R.id.et_url);
btn_show = (button) findViewbyId(R.id.btn_show);
tv_code = (TextView) findViewbyId(R.id.tv_code);
btn_show.setOnClickListener(new MyonClicklistener());
}
private class MyonClicklistener implements View.OnClickListener {
@Override
public void onClick(View v){
new Thread(){
public void run(){
String path = et_url.getText().toString().trim();
URL url = null;
try {
url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
int code = connection.getResponseCode();
if(code==200){
InputStream inputStream = connection.getInputStream();
String result = Utils.getStringFromStream(inputStream);
Message msg = new Message();
msg.obj=result;
handler.sendMessage(msg);
//tv_code.setText(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
}
這個代碼是main的代碼,還需要一個類,
public class Utils {
public static String getStringFromStream(InputStream inputStream) throws IOException {
byteArrayOutputStream baso = new byteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while((len = inputStream.read(buffer))!=-1){
baso.write(buffer, 0, len);
}
inputStream.close();
byte[] byteArray = baso.tobyteArray();
return new String(byteArray);
}
}
這個圖片是運行結(jié)果圖片。
一共需要這兩個類。
總結(jié),在鄭州app開發(fā)中,尤其需要注意:
1、聯(lián)網(wǎng)必須在子線程中操作。
2、子線程不能修改UI,修改UI必須在主線程進(jìn)行。
文章題目:android實現(xiàn)網(wǎng)頁源碼查看器
本文地址:http://www.rwnh.cn/article38/scdhsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、Google、做網(wǎng)站、營銷型網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)