Android 網(wǎng)絡(luò)html源碼查看器詳解及實例
Android 網(wǎng)絡(luò)html源碼查看器詳解及實例
IO字節(jié)流的數(shù)據(jù)傳輸了解
Handler的基本使用
1.作品展示
2.需要掌握的知識
FileInputStream,FIleOutputStream,BufferInputStream,BufferOutStream的讀寫使用與區(qū)別
//進(jìn)行流的讀寫
byte[] buffer = new byte[1024 * 8];
//創(chuàng)建一個寫到內(nèi)存的字節(jié)數(shù)組輸出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer,0,len);
byteArrayOutputStream.flush();//不斷的刷新緩存
}
byteArrayOutputStream.close();//進(jìn)行關(guān)閉流通道
result = byteArrayOutputStream.toString();//將寫入的字節(jié)流轉(zhuǎn)化為字符串
請求URL地址的基本步驟
使用UrlConnection請求一個url地址獲取內(nèi)容:
//1.創(chuàng)建一個Url對象
URL url = new URL(url_str);
//2.獲取一個UrlConnection對象
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//3.為UrlConnection對象設(shè)置一些請求的參數(shù),請求方式,連接的超時時間
connection.setRequestMethod("GET");//設(shè)置請求方式
connection.setConnectTimeout(1000*10);//設(shè)置超時時間
//4.在獲取url請求的數(shù)據(jù)前需要判斷響應(yīng)碼,200 :成功,206:訪問部分?jǐn)?shù)據(jù)成功 300:跳轉(zhuǎn)或重定向 400:錯誤 500:服務(wù)器異常
int code = connection.getResponseCode();
if(code == 200){
//5.獲取有效數(shù)據(jù),并將獲取的流數(shù)據(jù)解析成String
InputStream inputStream = connection.getInputStream();
String result = StreamUtils.streamToString(inputStream);
Handler消息機(jī)制的寫法與基本原理
1.主線程中創(chuàng)建一個Handler
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
};
};
2.重寫handler的handlermessage方法
3.子線程中創(chuàng)建一個Message對象,將獲取的數(shù)據(jù)綁定給msg
Message msg = new Message();
//另一種方式:Message msg = Messge.obtain;
msg.obj = result;
4.主線程中的handler對象在子線程中將message發(fā)送給主線程
handler.sendMessage(msg);
5.主線程中handlermessage方法接受子線程發(fā)來的數(shù)據(jù),就可以做更新UI的操作。
3.知識詳解
消息機(jī)制原理
1.Message:用來攜帶子線程中的數(shù)據(jù)。
2.MessageQueue:用來存放所有子線程發(fā)來的Message.
3.Handler:用來在子線程中發(fā)送Message,在主線程中接受Message,處理結(jié)果
4.Looper:是一個消息循環(huán)器,一直循環(huán)遍歷MessageQueue,從MessageQueue中取一個Message,派發(fā)給Handler處理。

handler原理.png
4.項目代碼
public class MainActivity extends AppCompatActivity {
private EditText et_url;
private Button btn_looksource;
private TextView tv_sourceshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1,找到控件ID
et_url = (EditText)findViewById(R.id.ev_url);
btn_looksource = (Button)findViewById(R.id.btn_looksource);
tv_sourceshow = (TextView)findViewById(R.id.source_show);
//2,設(shè)置點擊事件
btn_looksource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//3,獲取URL地址
final String url_str = et_url.getText().toString().trim();
//String results ="";
//3.1進(jìn)行判斷URL是否為空
if (TextUtils.isEmpty(url_str)) {
Toast.makeText(MainActivity.this, "url地址不能為空的", Toast.LENGTH_SHORT).show();
return;
}
System.out.println("oclick方法線程:"+Thread.currentThread().getName());
//創(chuàng)建子線程對象
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("oclick方法runnable線程:"+Thread.currentThread().getName());
//4,請求URL地址
//4.1創(chuàng)建一個URL對象
URL url = new URL(url_str);
//4.2獲取URLConnection對象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//4.3位URLconnection設(shè)置請求參數(shù)
connection.setRequestMethod("GET");
connection.setConnectTimeout(5 * 1000);
//4.4獲取請求響應(yīng)碼并進(jìn)行判斷
int code = connection.getResponseCode();
if (code == 200) {
//4.5獲取數(shù)據(jù)并且解析成字符串
InputStream inputStream = connection.getInputStream();
//4.6進(jìn)行獲取string數(shù)據(jù)讀出來的
String results = StreamUtils.streanTosting(inputStream);
//5,將數(shù)據(jù)顯示到TExtview 上
//tv_sourceshow.setText(results);
//☆☆☆3.子線中創(chuàng)建一個Message對象,為了攜帶子線程中獲取的數(shù)據(jù)給主線程。
//Message msg = new Message();
//msg.obj = result;//將獲取的數(shù)據(jù)封裝到msg中。
//☆☆☆4.使用handler對象將message發(fā)送到主線程。
//handler.sendMessage(msg);
Message msg = new Message();
msg.obj = results;
handler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
//☆☆☆1.在主線程中創(chuàng)建一個Handler對象
private Handler handler = new Handler(){
//☆☆☆2.重寫handler的handlermessage方法,用來接收子線程中發(fā)來的消息
public void handleMessage(android.os.Message msg) {
//☆☆☆5.接收子線程發(fā)送的數(shù)據(jù),處理數(shù)據(jù)。
String result = (String) msg.obj;
//☆☆☆6.當(dāng)前方法屬于主線程可以做UI的更新
//五.獲取服務(wù)器返回的內(nèi)容,顯示到textview上
tv_sourceshow.setText(result);
};
};
}
public class StreamUtils {
public static String streanTosting(InputStream inputStream){
String result = "";//保存字符串
try {
//進(jìn)行流的讀寫
byte[] buffer = new byte[1024 * 8];
//創(chuàng)建一個寫到內(nèi)存的字節(jié)數(shù)組輸出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer,0,len);
byteArrayOutputStream.flush();//不斷的刷新緩存
}
byteArrayOutputStream.close();//進(jìn)行關(guān)閉流通道
result = byteArrayOutputStream.toString();//將寫入的字節(jié)流轉(zhuǎn)化為字符串
}catch (Exception e){
e.printStackTrace();
}
return result;
}
}
5.反思總結(jié)
1.對應(yīng)web的http協(xié)議基本原理沒有了解,所以不太能理解網(wǎng)絡(luò)的傳輸
2.注意在配置文件中注冊獲取網(wǎng)絡(luò)權(quán)限,差點就gg了
3.開始接觸后臺的一些邏輯代碼有些不能很理解,不過這個過程中還是有收獲的,慢慢進(jìn)行吧
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android ListView中動態(tài)顯示和隱藏Header&Footer的方法
這篇文章主要介紹了Android ListView中動態(tài)顯示和隱藏Header&Footer的方法及footer的兩種正確使用方法,本文介紹的非常詳細(xì),具有參考借鑒價值,對listview header footer相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-08-08
android studio的使用sdk manager的方法
這篇文章主要介紹了android studio的使用sdk manager的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
android實現(xiàn)Uri獲取真實路徑轉(zhuǎn)換成File的方法
這篇文章主要介紹了android實現(xiàn)Uri獲取真實路徑轉(zhuǎn)換成File的方法,涉及Android操作路徑的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Android?NotificationListenerService?通知服務(wù)原理解析
這篇文章主要為大家介紹了Android?NotificationListenerService?通知服務(wù)原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
android ScrollView實現(xiàn)水平滑動回彈
這篇文章主要為大家詳細(xì)介紹了android ScrollView實現(xiàn)水平滑動回彈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

