Setting the proxy for android's DownloadManager class - android

I have an app with a WebView, which downloads videos and plays them in a VideoView.
To manage the downloads I use android's handy DownloadManager API.
Unfortunately in some situations I need to use a proxy.
I have successfully set up the proxy for the WebView using reflection as detailed in this stackoverflow question, but I am not sure how I can set the DownloadManager to use a proxy as well..
Is this possible?
If not, what are my alternatives?
Thanks

I couldn't find a way to do this with the DownloadManager so I ended up implementing my own (simplified) download manager using an AsyncTask.
It's possible then to pass a Proxy object to Url.openConnection as below:
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort));
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
Once you've got the proxied connection you can download content as per usual.

Related

android - how to hide url request

recently some one told me that he retrieves all the url requests from my android application and he sent them to me .
I'm using proguard in my application but he told me there are some ways that he can monitor the url requests .
I'm using URLConnection in my application .
how can I hide the url requests and somehow encrypt the urls so no one can read and access my url requestes ?
You can't. It's quite easy to use a proxy to catch all network traffic and check what you are using. If the problem is related with the content, then you can switch from http to https, but the host will be always visible. Actually as user I would be really scared about an app that want to hide such information however.
USE https instead of http, it is secured.
Try with HTTPS and add SSL(TSL) pinning to the network call.

How to get Android HttpURLConnection to handle a redirect

I'm making an app that basically downloads an image from the URL that I define and save it to the user's device. I'm doing this with:
URL url = new URL("http://speedtest.clotheslinedigital.com");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();
And then after checking that the storage is working properly and creating a blank file to save onto the device, I use a file output stream and an input stream to download the file to the device.
For testing purposes, I've just been using a Dropbox download link to an image and it's been working perfectly. However, in production, I want to use a DNS subdomain redirect to link to the file, instead of a hardcoded link in my app so that I don't have to update the app if I change the path to the file that the app needs to download.
For testing, I set a 301 redirect through my Godaddy DNS that redirects to the Dropbox link that I was using hardcoded for testing. The subdomain works perfectly when I enter the subdomain on my browser, but when I hardcore the subdomain URL into my app, nothing is downloaded and it just leaves behind a blank file (the one that was made after the request to be used to store the downloaded file).
I've tried using setFollowRedirects() and setting it to true, which I believe is the default but I tried anyway, and it still fails.
I thought maybe the issue was that the HttpURLConnection could not handle 301 response codes from the server, but I looked at the header of the Dropbox link and it 301 redirects the app a couple of times before downloading the file without issues.
Any ideas on how to fix this?
After some more research, it seems that I have to manually handle the redirect in my code. I basically did this by listening to the connection request's response code. If it's a 301 or 302 for a redirect, I have to grab the Location field from the header and feed that through the connection instead.
This is my code to do that:
boolean isRedirect;
do {
if (c.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || c.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
isRedirect = true;
String newURL = c.getHeaderField("Location");
c = (HttpURLConnection) new URL(newURL).openConnection();
} else {
isRedirect = false;
}
} while (isRedirect);
It's not necessary to encase the whole thing in an kind of loop, but doing so will allow the code to work properly, even if you have multiple redirects that need to be handled.

How to check whether url scheme is Http or Https and create URLConnection depending on it

So basically right now my app is configured to use https because in the "release" it will use a self signed certificate and obviously also use Https.
My current testsystem (few more features) doesn't use https but http instead. I thought it would be kinda nice to have some type of method to check whether the given URL is Http or Https and depending on the result create the right URLConnection.
My current problem is that I don't know what the method should exactly look like. I thought about using if-statements in the methods which connect to my server but there might be a better solution.
Help would be appreciated.
How about this:
URLUtil.isHttpUrl(String url)
URLUtil.isHttpsUrl(String url)
See also: http://developer.android.com/reference/android/webkit/URLUtil.html
If you don't want to do a manual check you can use a 3rd party library like this one: http://square.github.io/okhttp/ which allows you a simple:
Request request = new Request.Builder().url(url).build();
Response response = new OkHttpClient().newCall(request).execute();

Android connect to Mysql database tutorial

Does anybody know a complete and working tutorial about how to retrieve data from MYSQL and display it in Android? I'm asking this because all the tutorials I found are older than API 22 and from API 22 the HttpClient is deprecated. And I'm a new Android Developer so I can't write any code on my own. :)
HttpClient
Interface for an HTTP client. HTTP clients encapsulate a smorgasbord of objects required to execute HTTP requests while handling cookies, authentication, connection management, and other features. Thread safety of HTTP clients depends on the implementation and configuration of the specific client.
This interface was deprecated in API level 22.
Please use openConnection() instead.
Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.
For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward.
Please visit this webpage for further details.
http://android-developers.blogspot.in/2011/09/androids-http-clients.html
An URLConnection for HTTP (RFC 2616) used to send and receive data over the web. Data may be of any type and length. This class may be used to send and receive streaming data whose length is not known in advance.
Uses of this class follow a pattern:
Obtain a new HttpURLConnection by calling URL.openConnection() and casting the result to HttpURLConnection.
Read the response. Response headers typically include metadata such as the response body's content type and length, modified dates and session cookies. The response body may be read from the stream returned by getInputStream(). If the response has no body, that method returns an empty stream.
For example, to retrieve the webpage at http://www.android.com/:
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
}
Please visit this webpage for further details.
http://developer.android.com/reference/java/net/HttpURLConnection.html
urlconnection tutorials, can visit this websites
+http://www.vogella.com/tutorials/AndroidNetworking/article.html
+http://javatechig.com/android/android-networking-tutorial

Set httpRequest proxy in android

I want my android application (not my device) use a special proxy setting for all requests
Is there any way to set that?
Thanks
if you are using URLConnection you can create the connexion with this :URL.openConnection(Proxy)

Categories

Resources