我想讓我的Android應用程序建立到網站的連接,並通過代碼在後臺導航網站。據我瞭解,第一步是讓我想瀏覽該網站,我設法通過這個代碼做的HTML源代碼:使用Http POST和GET導航網站
public class HttpTest extends Activity {
private TextView tvCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.http_layout);
tvCode = (TextView)findViewById(R.id.tvHTMLCode);
String s = null;
try {
GetHtmlSourceCode html = new GetHtmlSourceCode();
html.execute("http://www.youtube.com");
s = html.get();
}
catch (Exception e) {
e.printStackTrace();
tvCode.setText("Error");
}
if (s != null)
tvCode.setText(s);
}
private class GetHtmlSourceCode extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
URL url = null;
HttpURLConnection conn = null;
String content = "";
try {
url = new URL(params[0]);
conn = (HttpURLConnection)url.openConnection();
InputStream in = conn.getInputStream();
int data = in.read();
while (data != -1) {
char c = (char) data;
data = in.read();
content += c;
}
}
catch (Exception e) {
e.printStackTrace();
return "error";
}
finally {
conn.disconnect();
return content;
}
}
}
}
(YouTube是僅僅作爲一個例子)。
從youtube.com獲取源代碼後,我希望我的應用在搜索框中輸入內容,然後單擊搜索按鈕。
根據我的理解,我需要發送一個POST請求到YouTube以填充搜索框,另一個POST按鈕,最後是一個GET,以獲得搜索結果頁面的html源代碼。然而,對這些問題似乎已經解決的HttpClient和HttpPost類的棄用,我有限的英語詞彙以及對該主題的一般無知使我很難自己找到解決方案。
有人可以幫忙嗎?
如果你想下載的網站整個網頁和鏈接,我建議你拿看看** webzip **。 – Soley
@Salivan我不想下載整個頁面。我只想通過代碼瀏覽他們。 – Steyiak