48

HttpURLConnectionContent-Typeに設定する方法を知っていますか?

次のコードは Blackberry にあり、Android に相当するものが必要です。

connection.setRequestProperty("content-type", "text/plain; charset=utf-8");
connection.setRequestProperty("Host", "192.168.1.36"); 
connection.setRequestProperty("Expect", "100-continue");

アンドロイドに適していますか?

お知らせ下さい。

4

2 に答える 2

73

本当にHttpURLConnectionを使用したい場合は、次のようなsetRequestPropertyメソッドを使用できます。

myHttpURLConnection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
myHttpURLConnection.setRequestProperty("Expect", "100-continue");

ただし、私があなたなら、Apache HTTP ライブラリの使用を検討します。それらは少しレベルが高く、使いやすいです。それらを使用すると、次のようになります。

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
}
于 2009-12-22T22:19:28.077 に答える
15
connection.setRequestProperty("Content-Type", "VALUE");
于 2009-12-22T10:02:31.843 に答える