欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中實(shí)現(xiàn)地址欄輸入網(wǎng)址能瀏覽該地址網(wǎng)頁源碼并操作訪問網(wǎng)絡(luò)

 更新時(shí)間:2013年06月14日 15:23:05   作者:  
Android中實(shí)現(xiàn)地址欄輸入網(wǎng)址能瀏覽該地址網(wǎng)頁源碼的效果,想必有很多朋友都不清楚吧,下面為大家詳細(xì)介紹下
 
首先實(shí)現(xiàn)簡單布局:
復(fù)制代碼 代碼如下:

<EditText
android:id="@+id/et_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:text="@string/url_text" >
<requestFocus android:layout_width="match_parent" />
</EditText>
<Button
android:id="@+id/btn_ie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_url"
android:onClick="sendHttp"
android:text="@string/btn_text" />
<ScrollView
android:id="@+id/sv_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btn_ie" >
<TextView
android:id="@+id/tv_ie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ie_text" />
</ScrollView>

在Stirng中
復(fù)制代碼 代碼如下:

<string name="url_text">http://luochuang.iteye.com/blog/1606231</string>
<string name="btn_text">瀏覽</string>
<string name="ie_text">顯示瀏覽網(wǎng)頁內(nèi)容</string>

新建類文件 :
 
首先MainActivity 中代碼 :
復(fù)制代碼 代碼如下:

public class MainActivity extends Activity {
// 聲明控件
public EditText et_url;
public TextView tv_ie;
// 網(wǎng)路操作類
public NetWorkUtils netWorkUtils;
private Handler handler;
public String content;
public static final int TEXT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取et_url對象
et_url = (EditText) findViewById(R.id.et_url);
tv_ie = (TextView) findViewById(R.id.tv_ie);
// 實(shí)例化
netWorkUtils = new NetWorkUtils(this);
// 實(shí)例化這個(gè)處理者
handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case TEXT:
tv_ie.setText(content);// 設(shè)置顯示的文本
break;
default:
break;
}
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendHttp(View v) {
// 設(shè)置網(wǎng)絡(luò)
boolean flag = netWorkUtils.setActiveNetWork();
if (flag) {
// run方法 執(zhí)行完畢 這個(gè)線程就消耗了
// 子線程
new Thread(new Runnable() {
@Override
public void run() {
send();
handler.sendEmptyMessage(TEXT);
}
}).start();
}
}
// 發(fā)送請求操作
@SuppressLint("NewApi")
public void send() {

// 獲取url的path路徑
String path = et_url.getText().toString();
if (path.isEmpty()) {
Toast.makeText(MainActivity.this, "訪問 的url地址不能為空",
Toast.LENGTH_LONG).show();
} else {
content = HttpUtils.sendGet(path);
}



/*// 設(shè)置網(wǎng)絡(luò)
netWorkUtils.setActiveNetWork();
// 獲取url的path路徑
String path = et_url.getText().toString();
if (path.isEmpty()) {
Toast.makeText(MainActivity.this, "訪問 的url地址不能為空",
Toast.LENGTH_LONG).show();
} else {
try {
// 設(shè)置訪問的url
URL url = new URL(path);
// 打開請求
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 設(shè)置請求的信息
httpURLConnection.setRequestMethod("GET");
// 判斷服務(wù)器是否響應(yīng)成功
if (httpURLConnection.getResponseCode() == 200) {
// 獲取響應(yīng)的輸入流對象
InputStream is = httpURLConnection.getInputStream();
// 字節(jié)輸出流
ByteArrayOutputStream bops = new ByteArrayOutputStream();
// 讀取數(shù)據(jù)的緩存區(qū)
byte buffer[] = new byte[1024];
// 讀取長度記錄
int len = 0;
// 循環(huán)讀取
while ((len = is.read(buffer)) != -1) {
bops.write(buffer, 0, len);
}
// 把讀取的內(nèi)容轉(zhuǎn)換成byte數(shù)組
byte data[] = bops.toByteArray();
// 把轉(zhuǎn)換成字符串
content = new String(data);
} else {
Toast.makeText(MainActivity.this, "服務(wù)器響應(yīng)錯(cuò)誤",
Toast.LENGTH_LONG).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}*/
}
}

復(fù)制代碼 代碼如下:

public class HttpUtils {


public static String sendGet(String path) {
String content = null;
try {
// 設(shè)置訪問url
URL url = new URL(path);
// 打開請求
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 設(shè)置請求的信息
httpURLConnection.setRequestMethod("GET");
// 設(shè)置請求是否超時(shí)時(shí)間
httpURLConnection.setConnectTimeout(5000);
// 判斷服務(wù)器是否響應(yīng)成功
if (httpURLConnection.getResponseCode() == 200) {
// 獲取響應(yīng)的輸入流對象
InputStream is = httpURLConnection.getInputStream();
byte data[] = StreamTools.isToData(is);
// 把轉(zhuǎn)換成字符串
content = new String(data);
// 內(nèi)容編碼方式
if (content.contains("gb2312")) {
content = new String(data, "gb2312");
}
}
// 斷開連接
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
}

復(fù)制代碼 代碼如下:

public class StreamTools {
public static byte[] isToData(InputStream is) throws IOException{

//字節(jié)輸出流
ByteArrayOutputStream bops = new ByteArrayOutputStream();
//讀取數(shù)據(jù)的緩存區(qū)
byte buffer[] = new byte[1024];
//讀取長度 的記錄
int len = 0;
//循環(huán)讀取
while((len = is.read(buffer)) != -1){
bops.write(buffer, 0, len);
}
//把讀取的內(nèi)容轉(zhuǎn)換成 byte數(shù)組
byte data[] = bops.toByteArray();

return data;

}
}

復(fù)制代碼 代碼如下:

public class NetWorkUtils {
private Context context;
// 網(wǎng)路鏈接管理對象
public ConnectivityManager connectivityManager;
public NetWorkUtils(Context context) {
this.context = context;
// 獲取網(wǎng)絡(luò)鏈接的對象
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
}
//public void setActiveNetWork() {
public boolean setActiveNetWork() {
boolean flag = false;
// 獲取可用的網(wǎng)絡(luò)鏈接對象
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
new AlertDialog.Builder(context)
.setTitle("網(wǎng)絡(luò)不可用")
.setMessage("可以設(shè)置網(wǎng)絡(luò)?")
.setPositiveButton("確認(rèn)",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(context, "點(diǎn)擊確認(rèn)",
Toast.LENGTH_LONG).show();
// 聲明意圖
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory("android.intent.category.LAUNCHER");
intent.setComponent(new ComponentName(
"com.android.settings",
"com.android.settings.Settings"));
intent.setFlags(0x10200000);
// 執(zhí)行意圖
context.startActivity(intent);
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
// 必須.show();
}).show();
}
//判斷網(wǎng)絡(luò)是否可用
if(networkInfo!=null){
flag=true;
}
return flag;
}
}

然后就是要在AndroidManifest.xml中添加 可以訪問網(wǎng)絡(luò)的權(quán)限
復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評論