質問

あなたは HttpURLConnectionのの上Content-Typeを設定する方法を知っていますか?

次のコードは、ブラックベリーにあると私はAndroidの同等を望んます:

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

それは右のアンドロイドのためですか?

助言してください。

役に立ちましたか?

解決

あなたが本当にのHttpURLConnection に使用したい場合は、

あなたが使用することができますのsetRequestProperty 方法:

myHttpURLConnection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
myHttpURLConnection.setRequestProperty("Expect", "100-continue");
私があなただったら、

しかし、私は<のhref =「http://developer.android.com/reference/org/apache/http/package-summary.html」のrel =「noreferrer」を使用してに見てね> 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
}

他のヒント

connection.setRequestProperty("Content-Type", "VALUE");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top