"Accept-language" not set in the request header while using URLConnection in android?

2.3k Views Asked by At

I am using URLConnection to download the html file from an android app. But I want the HTML content based on the device-locale.

Suppose user send the request from German locale then response should be in german language. I am expecting that URLConnection should send accept-language according to device-locale, similar to ajax calls. But accept-language header is not present in the request made by URLConnection.

Do I need to set request header explicitly or are there any alternative?

Thanks in advance.

1

There are 1 best solutions below

0
On

You can use any of following to get the device locale language in your format -

Locale.getDefault().getLanguage()       ---> en      
Locale.getDefault().getISO3Language()   ---> eng 
Locale.getDefault().getCountry()        ---> US 
Locale.getDefault().getISO3Country()    ---> USA 
Locale.getDefault().getDisplayCountry() ---> United States 
Locale.getDefault().getDisplayName()    ---> English (United States) 
Locale.getDefault().toString()          ---> en_US
Locale.getDefault().getDisplayLanguage()---> English

Source - Refer here

You can give Accept-Language header in your request like -

URL url = new URL("website url");
URLConnection urlconnection = url.openConnection();
urlconnection.setRequestProperty("Accept-Language", "en-GB");