Last active
October 3, 2023 09:52
-
-
Save hendrawd/c775d507807e200cfb30f53d6da62c00 to your computer and use it in GitHub Desktop.
Use proxy with Volley
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.android.volley.toolbox.HurlStack; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.InetSocketAddress; | |
import java.net.Proxy; | |
import java.net.URL; | |
/** | |
* @author hendrawd on 6/29/16 | |
*/ | |
public class ProxiedHurlStack extends HurlStack { | |
private static final String PROXY_ADDRESS = "some proxy address"; | |
private static final int PROXY_PORT = 80;//change with the port of the proxy | |
@Override | |
protected HttpURLConnection createConnection(URL url) throws IOException { | |
Proxy proxy = new Proxy(Proxy.Type.HTTP, | |
InetSocketAddress.createUnresolved(PROXY_ADDRESS, PROXY_PORT)); | |
return (HttpURLConnection) url.openConnection(proxy); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.Log; | |
import com.android.volley.Request; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.ionsoft.fotato.network.VolleySingleton; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
/** | |
* @author hendrawd on 6/29/16 | |
*/ | |
public class Test { | |
private static final String URL = "https://api.ipify.org/?format=json"; | |
public void showIpAddress(Context context) { | |
StringRequest stringRequest = new StringRequest(Request.Method.GET, | |
URL, | |
new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
try { | |
JSONObject jsonObject = new JSONObject(response); | |
Log.v(this.getClass().getSimpleName(), "My IP Address is = " + jsonObject.getString("ip")); | |
} catch (JSONException e) { | |
Log.v(this.getClass().getSimpleName(), "JSON Exception = " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
}, | |
new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Log.v(this.getClass().getSimpleName(), "Volley error when get IP Address = " + error.getMessage()); | |
} | |
}); | |
VolleySingleton.getInstance(context).addToRequestQueue(stringRequest); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.toolbox.Volley; | |
public class VolleySingleton { | |
private static VolleySingleton mInstance = null; | |
private RequestQueue mRequestQueue; | |
private static final String TAG = VolleySingleton.class.getSimpleName(); | |
private VolleySingleton(Context context) { | |
mRequestQueue = Volley.newRequestQueue(context, new ProxiedHurlStack()); | |
} | |
public static synchronized VolleySingleton getInstance(Context context) { | |
if (mInstance == null) { | |
mInstance = new VolleySingleton(context); | |
} | |
return mInstance; | |
} | |
private RequestQueue getRequestQueue() { | |
return this.mRequestQueue; | |
} | |
public <T> void addToRequestQueue(Request<T> req, Object tag) { | |
req.setTag(tag == null ? TAG : tag); | |
getRequestQueue().add(req); | |
} | |
public <T> void addToRequestQueue(Request<T> req) { | |
req.setTag(TAG); | |
getRequestQueue().add(req); | |
} | |
public void cancelPendingRequests(Object tag) { | |
if (mRequestQueue != null) { | |
mRequestQueue.cancelAll(tag); | |
} | |
} | |
public void cancelPendingRequestsNoTag() { | |
if (mRequestQueue != null) { | |
mRequestQueue.cancelAll(TAG); | |
} | |
} | |
public void clearVolleyCache() { | |
if (mRequestQueue != null) { | |
mRequestQueue.getCache().clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!