我想让我的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