views:

1237

answers:

4

Hi guys.

Do you know how to set Content-Type on HttpURLConnection?

Following code is on Blackberry and I want the Android equivalent:

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

Is it right for android?

Please advise.

+1  A: 
connection.setRequestProperty("Content-Type", "VALUE");
Ritz
Thanks for quick reply.I have a question ? I am trying to create http GET connection : connection.setRequestProperty("content-type", "text/plain; charset=utf-8");connection.setRequestProperty("Host", "192.168.1.36");connection.setRequestProperty("Expect", "100-continue");Is it right for android ?
AndroiDBeginner
Don't have much knowledge of android.Also, See the comment on your ques.
Ritz
A: 

Thanks for quick reply. I have a question ? I am trying to create http GET connection :

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

Is it right for android ?

AndroiDBeginner
You can modify your original question at the top to include this.
pkit
ok. iam gonna do
AndroiDBeginner
You should delete this answer of yours.
this. __curious_geek
ok. How to delete?
AndroiDBeginner
There should be a "delete" link just above the comments.
fiXedd
A: 

Any suggestion ? guys

AndroiDBeginner
This is a comment. Remove this answer and put this as comment in your question comments.
this. __curious_geek
+1  A: 

If you really want to use the HttpURLConnection you can use the setRequestProperty method like:

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

However, if I were you I'd look into using the Apache HTTP libraries. They're a little higher-level and easier to use. With them you would do it with something like:

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
}
fiXedd