8

It seems to me that there is a lot of confusing resources regarding the proxy topic on Android.

First of all, it seems that all the methods of the Proxy class are declared deprecated and it's suggested to:

"Use standard java vm proxy values to find the host, port and exclusion list. This call ignores the exclusion list."

The official java vm proxy values can be accessed in the following way:

System.getProperty("http.proxyHost")
System.getProperty("http.proxyPort")
System.getProperty("http.nonProxyHosts")

This could seem confirmed by the documentation of the ProxySelector class.

But trying on a real device or an emulator, these values seems to me always empty. After looking to the Android source code of the hidden ProxySelector activity, it seems that the proxy is saved into the secure settings of the system in the following way:

Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY, hostname);

And only an application signed by the OS provider can write to the secure settings. Developers can access these settings only in read mode in the following way:

Settings.Secure.getString(getApplicationContext().getContentResolver(),Settings.Secure.HTTP_PROXY);

Someone can clarify if this is the correct reading of how can be access the proxy settings into Android? (At least it seems to work). If this is the correct intepretation, why the documentation is so full of errors?

lechuckcaptain
  • 1,032
  • 1
  • 9
  • 25

2 Answers2

2

I don't know if it's the "right" way to access the proxy settings but it's the right and only way you should access the system "secure settings".

Maybe this is also interesting, looks like it makes things easier, especially if there are Wifi proxys (does Android support something like this?). At least it looks like great abstraction for the various android versions.

Luminger
  • 2,144
  • 15
  • 22
  • Oh Great! Actually it's my project. I hoped to find some answers from someone who knows the problem and the platform better than me. I hope not to be the leading expert on the problem! – lechuckcaptain Feb 17 '12 at 21:06
  • 1
    Oh, looks like you have won and have to do a little more research then ;) I'm sorry, have no clue then :/ – Luminger Feb 17 '12 at 21:10
2

For getting proxy values, accessing the System properties as you have done should work; it should not be necessary to access secure settings. If you cannot use the System properties to read proxy settings that were made through the normal device UI, then there is a problem. Proxies are per network type, so the APN and WiFi will have separate proxy settings.

Sparky
  • 8,437
  • 1
  • 29
  • 41
  • 1
    For Android 1.X and 2.X versions actually seems to work only the last example, getting the proxy settings from the security settings database. Since version 3.1 the user can enter the proxy settings for each Wi-Fi access point, but it's not provided (or at least it's not documented) a way for the developers to access these settings. I'm trying to build [this](http://code.google.com/p/android-proxy-library/) library in order to make it simple, but my question is: am I doing right or did I miss something in the documentation? – lechuckcaptain Feb 23 '12 at 13:05
  • 1
    Ahoy, Captain. I think what you get back from those System.getProperty() methods is only the default proxy for that scheme. If you get nothing back, perhaps one hasn't been defined. To get the proxy for a specific URL, do something like this: proxyList = myProxySelector.select(httpUri); – Sparky Feb 23 '12 at 14:22
  • Wow! It seems to work, at least on Android 3.X or greater! Illuminating comment! Thank you again Sparky! I'll try to write some additional documentation and examples, because I think that it's not really clear to a lot of other developers how to get and use these settings! – lechuckcaptain Feb 24 '12 at 21:42
  • I confirm that this seems to work only for Android 3.x or greater versions. I created a new question targeted for Android 2.x versions: http://stackoverflow.com/questions/9446871/how-the-users-can-set-the-androids-proxy-configuration-for-versions-2-x – lechuckcaptain Feb 25 '12 at 18:39