Merge pull request #1081 from eighthave/standardize-url-connections

use NetworkUtils.getHttpURLConnection for all URLConnections
This commit is contained in:
vshcherb 2015-02-20 21:36:50 +01:00
commit c523240209
6 changed files with 13 additions and 14 deletions

View file

@ -24,6 +24,7 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -235,8 +236,7 @@ public class MapTileDownloader {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
request.setError(false); request.setError(false);
try { try {
URL url = new URL(request.url); URLConnection connection = NetworkUtils.getHttpURLConnection(request.url);
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$ connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.setReadTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(CONNECTION_TIMEOUT);

View file

@ -19,6 +19,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -401,7 +402,7 @@ public class TileSourceManager {
public static List<TileSourceTemplate> downloadTileSourceTemplates(String versionAsUrl) { public static List<TileSourceTemplate> downloadTileSourceTemplates(String versionAsUrl) {
final List<TileSourceTemplate> templates = new ArrayList<TileSourceTemplate>(); final List<TileSourceTemplate> templates = new ArrayList<TileSourceTemplate>();
try { try {
URLConnection connection = new URL("http://download.osmand.net//tile_sources.php?" + versionAsUrl).openConnection(); URLConnection connection = NetworkUtils.getHttpURLConnection("http://download.osmand.net//tile_sources.php?" + versionAsUrl);
XmlPullParser parser = PlatformUtil.newXMLPullParser(); XmlPullParser parser = PlatformUtil.newXMLPullParser();
parser.setInput(connection.getInputStream(), "UTF-8"); parser.setInput(connection.getInputStream(), "UTF-8");
int tok; int tok;

View file

@ -14,6 +14,7 @@ import java.util.List;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.R; import net.osmand.plus.R;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -138,7 +139,7 @@ public class ContributionVersionActivity extends OsmandListActivity {
protected void executeThreadOperation(int operationId) throws Exception { protected void executeThreadOperation(int operationId) throws Exception {
if(operationId == DOWNLOAD_BUILDS_LIST){ if(operationId == DOWNLOAD_BUILDS_LIST){
URLConnection connection = new URL(URL_TO_RETRIEVE_BUILDS).openConnection(); URLConnection connection = NetworkUtils.getHttpURLConnection(URL_TO_RETRIEVE_BUILDS);
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(connection.getInputStream(), "UTF-8"); parser.setInput(connection.getInputStream(), "UTF-8");
int next; int next;
@ -164,7 +165,7 @@ public class ContributionVersionActivity extends OsmandListActivity {
} }
} }
} else if(operationId == INSTALL_BUILD){ } else if(operationId == INSTALL_BUILD){
URLConnection connection = new URL(URL_GET_BUILD + currentSelectedBuild.path).openConnection(); URLConnection connection = NetworkUtils.getHttpURLConnection(URL_GET_BUILD + currentSelectedBuild.path);
if(pathToDownload.exists()){ if(pathToDownload.exists()){
pathToDownload.delete(); pathToDownload.delete();
} }

View file

@ -17,6 +17,7 @@ import android.view.MenuItem.OnMenuItemClickListener;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -174,8 +175,7 @@ public class SearchAddressOnlineFragment extends Fragment implements SearchActiv
b.append("&q=").append(URLEncoder.encode(search, "UTF-8")); //$NON-NLS-1$ b.append("&q=").append(URLEncoder.encode(search, "UTF-8")); //$NON-NLS-1$
log.info("Searching address at : " + b); //$NON-NLS-1$ log.info("Searching address at : " + b); //$NON-NLS-1$
URL url = new URL(b.toString()); URLConnection conn = NetworkUtils.getHttpURLConnection(b.toString());
URLConnection conn = url.openConnection();
conn.setDoInput(true); conn.setDoInput(true);
conn.setRequestProperty("User-Agent", Version.getFullVersion((OsmandApplication) getActivity().getApplication())); //$NON-NLS-1$ conn.setRequestProperty("User-Agent", Version.getFullVersion((OsmandApplication) getActivity().getApplication())); //$NON-NLS-1$
conn.connect(); conn.connect();

View file

@ -16,6 +16,7 @@ import net.osmand.access.AccessibleToast;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.QuadRect; import net.osmand.data.QuadRect;
import net.osmand.data.RotatedTileBox; import net.osmand.data.RotatedTileBox;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick; import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -256,8 +257,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
b.append(",").append(topLatitude); //$NON-NLS-1$ b.append(",").append(topLatitude); //$NON-NLS-1$
try { try {
log.info("Loading bugs " + b); //$NON-NLS-1$ log.info("Loading bugs " + b); //$NON-NLS-1$
URL url = new URL(b.toString()); URLConnection connection = NetworkUtils.getHttpURLConnection(b.toString());
URLConnection connection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
XmlPullParser parser = Xml.newPullParser(); XmlPullParser parser = Xml.newPullParser();
parser.setInput(reader); parser.setInput(reader);

View file

@ -574,8 +574,7 @@ public class RouteProvider {
} }
uri.append("&fast=").append(params.fast ? "1" : "0").append("&layer=mapnik"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ uri.append("&fast=").append(params.fast ? "1" : "0").append("&layer=mapnik"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
log.info("URL route " + uri); log.info("URL route " + uri);
URL url = new URL(uri.toString()); URLConnection connection = NetworkUtils.getHttpURLConnection(uri.toString());
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", Version.getFullVersion(params.ctx)); connection.setRequestProperty("User-Agent", Version.getFullVersion(params.ctx));
DocumentBuilder dom = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder dom = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dom.parse(new InputSource(new InputStreamReader(connection.getInputStream()))); Document doc = dom.parse(new InputSource(new InputStreamReader(connection.getInputStream())));
@ -949,9 +948,7 @@ public class RouteProvider {
// .append("&language=").append(Locale.getDefault().getLanguage()); // .append("&language=").append(Locale.getDefault().getLanguage());
log.info("URL route " + request); log.info("URL route " + request);
URI uri = URI.create(request.toString()); URLConnection connection = NetworkUtils.getHttpURLConnection(request.toString());
URL url = uri.toURL();
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", Version.getFullVersion(params.ctx)); connection.setRequestProperty("User-Agent", Version.getFullVersion(params.ctx));
DocumentBuilder dom = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder dom = DocumentBuilderFactory.newInstance().newDocumentBuilder();