Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley behind a proxy server

I am new to Volley Networking Library (of Android). I have observed that the Request function takes URL as the parameter instead of server name and port. Is there any way for making Volley request to go through a Proxy Server of my choice if I mention the server name and the port?

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

I know that we can make use of server and port info while building the URL but is there a way other than this to make sure that the requests go through a Proxy mentioned by us ?

For example: How do I make HttpURLConnection use a proxy? Here is a method to ensure that HttpURLConnection uses a proxy. I am looking for similar answers for Volley.

like image 957
user3683906 Avatar asked May 28 '14 14:05

user3683906


1 Answers

Volley doesn't offer any direct methods to set proxy but there is a way.

Create a custom class extending HurlStack, say ProxiedHurlStack

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import com.android.volley.toolbox.HurlStack;

public class ProxiedHurlStack extends HurlStack {

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {

        // Start the connection by specifying a proxy server
        Proxy proxy = new Proxy(Proxy.Type.HTTP,
                InetSocketAddress.createUnresolved("192.168.1.11", 8118));//the proxy server(Can be your laptop ip or company proxy)
        HttpURLConnection returnThis = (HttpURLConnection) url
                .openConnection(proxy);

        return returnThis;
    }
}

Now initialize your queue using:

mRequestQueue = Volley.newRequestQueue(context, new ProxiedHurlStack());

Courtesy: http://d.hatena.ne.jp/esmasui/20130613/1371126800

like image 148
Praveen Avatar answered Sep 18 '22 14:09

Praveen