當前位置:首頁 » 安卓系統 » android獲取網頁源碼

android獲取網頁源碼

發布時間: 2022-06-26 04:14:10

A. android webview載入某個網頁,之後通過這個網頁調到了另一個頁面,怎麼獲取這個頁面的網址和源碼

mWebView.setWebViewClient(new WebViewClient(){
// 這個方法在用戶試圖點開頁面上的某個鏈接時被調用
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url!=null) {
// 如果想繼續載入目標頁面則調用下面的語句
// view.loadUrl(url);
// 如果不想那url就是目標網址,如果想獲取目標網頁的內容那你可以用HTTP的API把網頁扒下來。
}
// 返回true表示停留在本WebView(不跳轉到系統的瀏覽器)
return true;
}
});

B. android怎麼獲取JS執行之後的網頁源代碼

可以試用phantomjs載入網頁,執行js,然後獲取執行後的網頁代碼。
官網: http://phantomjs.org/

C. android怎麼獲取網頁數據

下面介紹三種獲取網頁數據的代碼
例子來自於android學習手冊,android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼
//第一種
/**獲取參數(ArrayList<NameValuePair> nameValuePairs,String url)後post給遠程伺服器
* 將獲得的返回結果(String)返回給調用者
* 本函數適用於查詢數量較少的時候
*/
public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){
String result = "";
String tmp= "";
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
return "Fail to establish http connection!";
}

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();

tmp=sb.toString();
}catch(Exception e){
return "Fail to convert net stream!";
}

try{
JSONArray jArray = new JSONArray(tmp);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Iterator<?> keys=json_data.keys();
while(keys.hasNext()){
result += json_data.getString(keys.next().toString());
}
}
}catch(JSONException e){
return "The URL you post is wrong!";
}

return result;
}

//第二種
/**獲取參數指定的網頁代碼,將其返回給調用者,由調用者對其解析
* 返回String
*/
public String posturl(String url){
InputStream is = null;
String result = "";

try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
return "Fail to establish http connection!"+e.toString();
}

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();

result=sb.toString();
}catch(Exception e){
return "Fail to convert net stream!";
}

return result;
}

//第三種
/**獲取指定地址的網頁數據
* 返回數據流
*/
public InputStream streampost(String remote_addr){
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL(remote_addr);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
inStream = httpConnection.getInputStream();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inStream;

D. 游戲軟體怎麼查看源代碼

源代碼是看不成的,因為游戲軟體打包好做成app的話,是沒法看源碼的,雖然存在一些特殊情況下,我們可以推測出exe程序是用什麼程序寫的。但是多數情況下,我們是無法只根據一個exe程序就判斷出來的。

根據exe程序我們是無法直接得到程序的源碼的。雖然也有一些用於逆向工程的辦法,但那不可能把已經是exe的程序反回到它原始的源碼情況。而且這些工具都很難用。你可以用「反編譯」搜到很多工具,但是說實話,即便是這方面的專家,要看懂反編譯以後的程序也不是一件輕松的事情。

E. android studio 怎麼抓去網頁媒體資源

首先,也是很重要的一步,就是下載jar包,丟到libs裡面
Android studio玩家可以不下載jar包,在Gradle裡面加入
dependencies {undefined
compile 'org.jsoup:jsoup:1.9.2'
}復制代碼
然後,找到你心儀的網頁去抓取數據
這里我們我繼續使用美食的網頁,然後右鍵查看網頁源碼,或者按F12,接下來可以看到一大堆標簽:
Paste_Image.png
找到需要的,例如上圖這個 「美食天下」 ,可以看到 「美食天下」 是放在以
為節點的 中,要獲取這個「美食天下」,代碼可以這樣寫:
try {undefined
//從一個URL載入一個Document對象。
Document doc = Jsoup.connect("http://home.meishichina.com/show-top-type-recipe.html").get();
//選擇「美食天下」所在節點
Elements elements = doc.select("div.top-bar");
//列印 a標簽裡面的title
Log.i("mytag",elements.select("a").attr("title"));
}catch(Exception e) {undefined
Log.i("mytag", e.toString());
}復制代碼
接下來看一下列印出來的結果:
Paste_Image.png
Jsoup.connect(String url)方法從一個URL載入一個Document對象。如果從該URL獲取HTML時發生錯誤,便會拋出 IOException,應適當處理。
一旦擁有了一個Document,你就可以使用Document中適當的方法或它父類 Element和Node中的方法來取得相關數據。
public class Element extends Node
public class Document extends Element復制代碼
很多文章都是說一大堆原理然後放出一個簡單的例子,就跟我上面簡單的打了一個log一樣,然後發現用起來的時候是沒那麼簡單的。為了大家能不看文檔也可以直接使用(並且看不懂那一大堆標簽也可以用),我決定再舉一個例子(其實也就是比上面多打幾個log):
下圖紅色框框是我們要獲取的數據,可以看到他們對應的節點就是藍色圓圈裡面的
Paste_Image.png
廢話不多說上代碼
try {undefined
//還是一樣先從一個URL載入一個Document對象。
Document doc = Jsoup.connect("http://home.meishichina.com/show-top-type-recipe.html").get();
//「椒麻雞」和它對應的圖片都在復制代碼

Elements titleAndPic = doc.select("div.pic");
//使用Element.select(String selector)查找元素,使用Node.attr(String key)方法取得一個屬性的值
Log.i("mytag", "title:" + titleAndPic.get(1).select("a").attr("title") + "pic:" + titleAndPic.get(1).select("a").select("img").attr("data-src"));
//所需鏈接在
中的a標簽裡面
Elements url = doc.select("div.detail").select("a");
Log.i("mytag", "url:" + url.get(i).attr("href"));
//原料在

Elements burden = doc.select("p.subcontent");
//對於一個元素中的文本,可以使用Element.text()方法
Log.i("mytag", "burden:" + burden.get(1).text());
}catch(Exception e) {undefined
Log.i("mytag", e.toString());
}
大功告成,接下來看看log
Paste_Image.png
沒有問題!那麼教學可以結束了!
注意:
Jsoup.connect(String url)方法不能運行在主線程,否則會報NetworkOnMainThreadException

F. Android studio中,用get方法獲取網頁源代碼,怎麼做

< form id="form1" method="get" runat="server"> get方法提交表單 < div> 你的名字< asp:TextBox ID="name" runat="server">< /asp:TextBox>< br /> < br /> 你的網站< asp:TextBox ID="website" runat="server">< /asp:TextBox>< br /> < br /> < br /> < asp:Button ID="Button1" runat="server" Text="send" />< br /> < br /> < br /> 學習request 和 response的用法< br /> < br /> < br /> < /div> < /form> ----------------------------------------------------------------------------------------------------------- < form id="form2" method="post" runat="server"> post方法提交表單 < div> 你的名字< asp:TextBox ID="name2" runat="server">< /asp:TextBox>< br /> < br /> 你的網站< asp:TextBox ID="website2" runat="server">< /asp:TextBox>< br /> < br /> < br /> < asp:Button ID="Button2" runat="server" Text="send" />< br /> < br /> < br /> 學習request 和 response的用法< br /> < br /> < br /> < /div> < /form> 1. get是從伺服器上獲取數據,post是向伺服器傳送數據。 2. get是把參數數據隊列加到提交表單的ACTION屬性所指的URL中,值和表單內各個欄位一一對應,在URL中可以看到。post是通過HTTP post機制,將表單內各個欄位與其內容放置在HTML HEADER內一起傳送到ACTION屬性所指的URL地址。用戶看不到這個過程。 3. 對於get方式,伺服器端用Request.QueryString獲取變數的值,對於post方式,伺服器端用Request.Form獲取提交的數據。 4. get傳送的數據量較小,不能大於2KB。post傳送的數據量較大,一般被默認為不受限制。 5. get安全性非常低,post安全性較高。但是執行效率卻比Post方法好。 建議: 1、get方式的安全性較Post方式要差些,包含機密信息的話,建議用Post數據提交方式; 2、在做數據查詢時,建議用Get方式;而在做數據添加、修改或刪除時,建議用Post方式

G. Android 在WebView中通過javascript獲取網頁源碼,並在TextView或者在EditText中顯示問題

webview js之間的交互,項目中馬上用到。

JS調用java代碼效果圖


index.html代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd";><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" language="javascript"> var share = JSON.stringify({"title": "sinodata",
"desc": "ios",
"shareUrl": "http://www.sinodata.com.cn"
});

function sendInfoToJava(){
window.AndroidWebView.showInfoFromJs(share);
}

<!--在android代碼中調用此方法-->
function showInfoFromJava(msg){
alert("showInfoFromJava:"+msg);
} </script></head><body la><div id='b'> <input onclick="sendInfoToJava()" type="button" value="sendInfoToJava"/></div></body></html>
布局代碼:

<?xml version="1.0" encoding="utf-8"?><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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.chenjifang.webview.MainActivity"> <Button android:id="@+id/test_btn" android:text="代碼中調用web js代碼傳遞參數" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/test_edt" android:layout_width="match_parent" android:layout_height="wrap_content" /><WebView android:id="@+id/test_webview" android:layout_width="match_parent" android:layout_height="400dp"></WebView></LinearLayout>
java代碼:

public class MainActivity extends AppCompatActivity {private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.test_webview); //設置WebView支持JavaScript mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("file:///android_asset/index.html"); mWebView.addJavascriptInterface(new JsInterface(this), "AndroidWebView"); //添加客戶端支持 mWebView.setWebChromeClient(new WebChromeClient()); findViewById(R.id.test_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
sendInfoToJs(); }
}); } private class JsInterface { private Context mContext; public JsInterface(Context context) { this.mContext = context; } //在js中調用window.AndroidWebView.showInfoFromJs(name),便會觸發此方法。 @JavascriptInterface public void showInfoFromJs(String share) {
Toast.makeText(mContext, share, Toast.LENGTH_SHORT).show(); }
} //在java中調用js代碼 public void sendInfoToJs() {
String msg = ((EditText)findViewById(R.id.test_edt)).getText().toString(); //調用js中的函數:showInfoFromJava(msg) mWebView.loadUrl("javascript:showInfoFromJava('" + msg + "')"); }
總結下,java代碼中要設置webview對javascript的支持,addJavascriptInterface(new JsInterface(this), "AndroidWebView");//這句代碼中的第二個參數是在js訪問方法的地址。
window.AndroidWebView.showInfoFromJs(share);

H. android獲取網頁源碼,只能獲取當前屏幕大小的HTML源碼,後面的要怎麼顯示出來

在Androidmanifest.xml里添加以下兩行就ok.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
then.就口以添加關於抓取網頁的代碼了。主要就是倆函數,一個負責連接網頁(testGetHtml()),一個用於讀取源碼(readStream()):
public static byte[] readStream(InputStream inputStream) throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
inputStream.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
}
public static String testGetHtml(String urlpath) throws Exception {
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6 * 1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream inputStream = conn.getInputStream();
byte[] data = readStream(inputStream);
String html = new String(data);
return html;
}
return null;
}
用的時候注意URL要寫全,testGetHtml("http://www..com/")這樣即可。
源碼會存在一個String變數中

I. 如何獲取android源代碼

當前的Android代碼託管在兩個方:https://github.com/android 和https://android.googlesource.com之前在 android.git.kernel.org上也有託管,不過現在重定向到了https://android.googlesource.com好在都支持git訪問。

google提供的repo工具實際上是一個內部操作git工具來簡化操作Android源碼的Python腳本。經過嘗試,直接使用git工具在ubuntu下可以實現cloneAndroid源碼。下面介紹一下方法:

1.獲取當前的在github上託管的Androidgitrepositories:

github頁面為:https://github.com/android/following。不過這個頁面不支持通過wget"https://github.com/android/following"或者curl"https://github.com/android/following"的方式訪問,錯誤信息如下:

這個時候需能做的只能是"tryagain"了。

需要說明的是"不要試圖同時並發執行多個gitclone命令",這樣會導致大量出現上面貼圖中的錯誤,另外,整個clone過程中耗時最多的gitrepository如下:

kernel_common.gitkernel_msm.gitplatform_frameworks_base.gitplatform_prebuilt.git其中platform_prebuilt.git是google提供的預編譯好的二進制文件,包含:各種庫文件,jar包,可執行程序等等,如果只是閱讀Android源代碼,這個gitrepository可以不用clone.

J. 有沒有可以在 iOS 和 Android 上查看網頁源碼的瀏覽器

Android
上的
Firefox
可以在地址欄里的原URL前加
view-source:
即可查看源代碼。
加裝這個插件會新開一個標簽來查看源代碼,更方便些。
View
Source
Mobile
View
Source
Mobile
::
Add-ons
for
Firefox
for
Android
另外
@Bill
Cheng
:
Android
上的
Firefox
暫時還沒有
FireBug

熱點內容
html5android教程視頻下載 發布:2024-04-26 03:09:59 瀏覽:866
伺服器的描述是什麼 發布:2024-04-26 03:08:32 瀏覽:393
個人加密 發布:2024-04-26 03:01:23 瀏覽:519
linuxusbgadget 發布:2024-04-26 02:52:54 瀏覽:303
我的世界空島世界伺服器地址 發布:2024-04-26 01:39:08 瀏覽:248
尼爾機械紀元加密 發布:2024-04-26 01:37:11 瀏覽:867
在控制台輸出sql語句 發布:2024-04-26 01:08:12 瀏覽:432
動畫java 發布:2024-04-26 01:02:40 瀏覽:12
得力文件夾5302 發布:2024-04-26 00:21:32 瀏覽:91
您的個人文件夾 發布:2024-04-26 00:03:12 瀏覽:68