代理服务器后面的Volley [英] Volley behind a proxy server
问题描述
我是Volley Networking Library(Android版)的新手.我观察到Request函数将URL作为参数而不是服务器名称和端口.如果我提到服务器名称和端口,是否有任何方法可以使Volley请求通过我选择的代理服务器?
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>() {
我知道我们可以在构建URL时使用服务器和端口信息,但是还有其他方法可以确保请求通过我们提到的Proxy吗?
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 ?
例如:如何让HttpURLConnection使用代理?/a>这是确保HttpURLConnection使用代理的方法.我正在为Volley寻找类似的答案.
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.
推荐答案
Volley不提供任何直接设置代理的方法,但是有一种方法.
Volley doesn't offer any direct methods to set proxy but there is a way.
创建一个扩展HurlStack的自定义类,例如ProxiedHurlStack
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());
礼貌: http://d.hatena.ne.jp/esmasui/20130613/1371126800
这篇关于代理服务器后面的Volley的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!