你知道如何设置吗Content-Type
on HttpURL连接 http://developer.android.com/reference/java/net/HttpURLConnection.html?
以下代码适用于 Blackberry,我想要 Android 上的等效代码:
connection.setRequestProperty("content-type", "text/plain; charset=utf-8");
connection.setRequestProperty("Host", "192.168.1.36");
connection.setRequestProperty("Expect", "100-continue");
适合安卓吗?
请指教。
如果你真的想使用HttpURL连接 http://developer.android.com/reference/java/net/HttpURLConnection.html你可以使用设置请求属性 http://developer.android.com/reference/java/net/URLConnection.html#setRequestProperty%28java.lang.String,%20java.lang.String%29方法如:
myHttpURLConnection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
myHttpURLConnection.setRequestProperty("Expect", "100-continue");
但是,如果我是你,我会考虑使用Apache HTTP 库 http://developer.android.com/reference/org/apache/http/package-summary.html。它们的级别更高一些并且更易于使用。与他们一起,你可以这样做:
HttpGet get = new HttpGet("http://192.168.1.36/");
get.setHeader("Content-Type", "text/plain; charset=utf-8");
get.setHeader("Expect", "100-continue");
HttpResponse resp = null;
try {
HttpClient httpClient = new DefaultHttpClient();
resp = httpClient.execute(get);
} catch (ClientProtocolException e) {
Log.e(getClass().getSimpleName(), "HTTP protocol error", e);
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Communication error", e);
}
if (resp != null) {
// got a response, do something with it
} else {
// there was a problem
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)