Android中的HTTP连接
如何创建Http连接以将网页内容检索到我的android?请发布此示例代码。其http客户端,获取示例,它可能会有所帮助Android中的HTTP连接,android,http,Android,Http,如何创建Http连接以将网页内容检索到我的android?请发布此示例代码。其http客户端,获取示例,它可能会有所帮助 HttpClient client; HttpGet method; String url; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); url="enter your url here"
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数据,我尝试了这个示例。我在手机中获取XML数据,因此如何在不使用XML标记的情况下转换为真实数据?我没有尝试在这里查找,我认为这对您有帮助。