本文介绍了如何在Android应用程序中的HttpGet方法中添加Header?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到了许多addHeader()的例子,但只使用了HttpPost方法。我正在寻找一个在Android应用程序中使用addHeader()实现HttpGet方法的简单示例。
I saw many examples to addHeader() but only using HttpPost method. I am looking for a simple example having implementation of HttpGet methos with addHeader() in android application.
提前致谢
推荐答案
这是HTTPGET请求的简单示例,并且使用addheader
This is Simple Example with HTTPGET requrest and using with addheader
public String httpGet(String s) {
String url = s;
StringBuilder body = new StringBuilder();
httpclient = new DefaultHttpClient(); // create new httpClient
HttpGet httpGet = new HttpGet(url); // create new httpGet object
httpGet.setHeader("If-Modified-Since","11/26/2012");
try {
response = httpclient.execute(httpGet); // execute httpGet
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
// System.out.println(statusLine);
body.append(statusLine + "\n");
HttpEntity e = response.getEntity();
String entity = EntityUtils.toString(e);
body.append(entity);
} else {
body.append(statusLine + "\n");
// System.out.println(statusLine);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection(); // stop connection
}
return body.toString(); // return the String
}
为了完全理解get和post with header can还可以访问
for complete understanding of get and post with header you can also go to HttpGet and HttpPost
这篇关于如何在Android应用程序中的HttpGet方法中添加Header?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!