天天看點

html 檢視程式代碼,檢視HTML代碼

1.[檔案] activity_main.xml ~ 1KB     下載下傳(0)

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/et_address"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="3"

android:singleLine="true"

/>

android:id="@+id/btn_query"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="查詢"

android:onClick="onClick"

/>

android:id="@+id/et_content"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="top"

android:singleLine="false"

/>

2.[檔案] MainActivity.java ~ 3KB     下載下傳(0)

package com.example.jcker_llin.html;

import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

public class MainActivity extends Activity {

private EditText et_address;

private EditText et_content;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

et_address= (EditText) findViewById(R.id.et_address);

et_content= (EditText) findViewById(R.id.et_content);

}

public void onClick(View view){

//在編輯框輸入網址

String urlStr=et_address.getText().toString();

new AsyncTask(){

@Override

protected String doInBackground(String... params) {

//字元緩沖區

StringBuffer buffer=new StringBuffer();

try {

URL url=new URL("http://"+params[0]+":80");

//擷取http連接配接對象

HttpURLConnection conn= (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

///狀态碼

int code=conn.getResponseCode();

if(code==200) {//請求成功

//擷取響應消息的實體内容

InputStreamReader reader = new InputStreamReader(conn.getInputStream(), "UTF-8");

char[] charArr = new char[1024 * 8];

int len = 0;

while ((len = reader.read(charArr)) != -1) {

//字元數組轉換字元串

String str = new String(charArr, 0, len);

//追加字元串

buffer.append(str);

}

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return buffer.toString();

}

@Override

protected void onPostExecute(String s) {

//傳回HTML代碼

et_content.setText(s);

}

}.execute(urlStr);

}

}

3.[檔案] AndroidManifest.xml ~ 769B     下載下傳(0)

package="com.example.jcker_llin.html" >

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme" >

4.[圖檔] 26ERZ6G{F](RMU%VMHYFVBY.png

html 檢視程式代碼,檢視HTML代碼