時(shí)間:2023-05-28 08:51:02 | 來源:網(wǎng)站運(yùn)營
時(shí)間:2023-05-28 08:51:02 來源:網(wǎng)站運(yùn)營
Android網(wǎng)絡(luò)開發(fā):<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" > <WebView android:id="@+id/wv_first" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
private WebView wv_first;wv_first = (WebView) findViewById(R.id.wv_first); // getSettings()設(shè)置瀏覽器的屬性 // setJavaScriptEnabled(true)讓W(xué)ebView支持JavaScript腳本 wv_first.getSettings().setJavaScriptEnabled(true); // setWebViewClient(new WebViewClient()) // 這段代碼的作用是當(dāng)我們從一個(gè)網(wǎng)頁跳轉(zhuǎn)到另外一個(gè)網(wǎng)頁的時(shí)候,目標(biāo) // 網(wǎng)頁仍然在WebView中顯示 // 而不是打開系統(tǒng)瀏覽器 wv_first.setWebViewClient(new WebViewClient()); //傳入網(wǎng)址,打開網(wǎng)頁 wv_first.loadUrl("http://www.baidu.com");
<uses-permission android:name="android.permission.INTERNET"/>
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_request_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView>
@Override public void run() { URL url; HttpURLConnection connection; try { // 先new出一個(gè)URL對象,傳入網(wǎng)絡(luò)地址 // 調(diào)用openConnection()方法獲取到HttpURLConnection對象 url = new URL("http://www.baidu.com"); connection = (HttpURLConnection) url.openConnection(); // 設(shè)置HTTP請求所使用的方法,GET或者POST // GET表示從服務(wù)器獲取數(shù)據(jù) // POST表示向服務(wù)器提交數(shù)據(jù) connection.setRequestMethod("GET"); // 下面使一些自由的定制,比如設(shè)置連接超時(shí),讀取超時(shí)的毫秒數(shù),以及服務(wù)器希望得到的一些消息頭 connection.setConnectTimeout(8000); connection.setReadTimeout(8000); // 接下來利用輸入流對數(shù)據(jù)進(jìn)行讀取 InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line); } // 讀取數(shù)據(jù)完畢,接下來將數(shù)據(jù)傳送到Handler進(jìn)行顯示 Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); //最后關(guān)閉HTTP連接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } }
@Override public void handleMessage(Message msg) { if(msg.what==SHOW_REQUEST){ String response=(String) msg.obj; //在這里進(jìn)行UI操作將結(jié)果顯示到UI上 tv_request_text.setText(response); } }
<uses-permission android:name="android.permission.INTERNET"/>
// Handler用來處理網(wǎng)絡(luò)訪問請求之后得到數(shù)據(jù)的顯示 MyHandler handler = new MyHandler(tv_request_text); // 創(chuàng)建訪問網(wǎng)絡(luò)的線程對象 final SendHttpRequestThread request = new SendHttpRequestThread(handler); // 設(shè)置按鈕點(diǎn)擊事件 btn_request.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 開啟線程 request.start(); } });
@Override public void run() { // 因?yàn)镠ttpClient是一個(gè)接口,所以無法直接創(chuàng)建它的對象,一般是用 HttpClient client = new DefaultHttpClient(); // 想要發(fā)起一條GET請求就創(chuàng)建一個(gè)HttpGet對象 // 并傳入目標(biāo)網(wǎng)絡(luò)地址,調(diào)用HttpClient的execute()方法傳入HttpGet對象 HttpGet httpGet = new HttpGet("http://www.baidu.com"); try { // 執(zhí)行execute()方法后會(huì)返回HttpResponse對象,服務(wù)器返回的所有信息就包含在這里面了 HttpResponse response = client.execute(httpGet); // 接下來取出服務(wù)器返回的狀態(tài)碼,如果是200就說明請求和響應(yīng)都成功了 if (response.getStatusLine().getStatusCode() == 200) { // 請求和響應(yīng)都成功了,取出返回的具體內(nèi)容 // response的getEntity()得到一個(gè)HttpEntity對象 // EntityUtils這個(gè)工具類的toString()方法將得到的內(nèi)容轉(zhuǎn)化成字符串 // 傳入"utf-8"的目的是在服務(wù)器返回中文的時(shí)候防止亂碼 HttpEntity entity = response.getEntity(); String responsetext = EntityUtils.toString(entity, "utf-8"); // 接下來將數(shù)據(jù)傳送到Handler進(jìn)行顯示 Message message = new Message(); message.what = SHOW_REQUEST; message.obj = responsetext; handler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); } }
// Handler用來處理網(wǎng)絡(luò)訪問請求之后得到數(shù)據(jù)的顯示 MyHandler handler = new MyHandler(tv_request_text); //創(chuàng)建HttpCliect訪問網(wǎng)絡(luò)的線程對象 final SendClientRequestThread requestThread=new SendClientRequestThread(handler); // 設(shè)置按鈕點(diǎn)擊事件 btn_request.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 開啟線程 requestThread.start(); } });
sc delete apache
關(guān)鍵詞:網(wǎng)絡(luò)
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。