如何创建一个Http Connection来检索我的android的网页内容?请发布示例代码。
答案 0 :(得分:3)
它的http客户端,得到它可能有帮助的例子
HttpClient client;
HttpGet method;
String url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
url="enter your url here";
method = new HttpGet(url);
try {
client.execute(method);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(R.layout.main);
}
public void executeHttpGet() throws Exception {
BufferedReader in = null;
try {
client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(method);
in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
System.out.println(page);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
它在预览中访问XML数据我有这个例子试试。