2009-12-22 43 views
38

Content-Type'u HttpURLConnection olarak nasıl ayarlayacağınızı biliyor musunuz?HttpURLConnection'da İçerik Türü nasıl ayarlanır?

ardından kod Blackberry ve ben Android eşdeğer istiyorum:

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

doğru android için uygun mu?

Lütfen bildiriniz.

+0

Bir GET isteği için belirtmek için doğru üstbilgiyi arıyordum, bu yüzden soruyu yanıtlayarak sorumu yanıtladım. – Grubsnik

+0

Merhaba, konunuzla ilgili bir sorum var ... "connection.setRequestProperty (" Expect "," 100-continue ") ile ilgili genel bir fikir verebilir misiniz?" Prosedürünü etkiler mi? İhtiyacın var mı? 100 yanıt bekle, sonra başka bir operasyon yap ve sonra 200 yanıt bekle? – Josh

cevap

59

Gerçekten kullanmak istiyorsanız HttpURLConnection sen gibi setRequestProperty yöntemi kullanabilirsiniz: Sana olsaydı

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

Ancak, Apache HTTP libraries kullanarak içine bakmak. Biraz daha üst düzey ve kullanımı daha kolay. Onlarla sizin gibi bir şey ile yaparsınız:

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 
} 
+10

Gerçekten tavsiye ettiği gibi Jesse Wilson tarafından önerilen UrlConnection kullanılmalıdır - http://android-developers.blogspot.com/2011/09/androids-http-clients.html –

13
connection.setRequestProperty("Content-Type", "VALUE"); 
+0

Hızlı yanıt için teşekkürler. Bir sorum var? http GET bağlantısı oluşturmaya çalışıyorum: connection.setRequestProperty ("content-type", "text/plain; charset = utf-8"); connection.setRequestProperty ("Host", "192.168.1.36"); connection.setRequestProperty ("Expect", "100-devam"); Android için doğru mu? – AndroiDBeginner

+0

Android hakkında çok fazla bilginiz yok. Ayrıca, Sorularınız hakkındaki yorumları görün. – Rites

İlgili konular