Android: Unable to make httprequest behind firewall
Asked Answered
R

2

1

The standard getUrlContent works welll when there is no firewall. But I got exceptions when I try to do it behind a firewall.

I've tried to set "http proxy server" in AVD manager, but it didn't work. Any idea how to correctly set it up?

and btw: from android documentation "You can use the -verbose-proxy option to diagnose proxy connection problems." -verbose-proxy is not a valid option at all.

protected static synchronized String getUrlContent(String url) throws ApiException {
    if(url.equals("try")){
        return "thanks";

    }
    if (sUserAgent == null) {
        throw new ApiException("User-Agent string must be prepared");
    }

    // Create client and set our specific user-agent string
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    request.setHeader("User-Agent", sUserAgent);

    try {
        HttpResponse response = client.execute(request);

        // Check if server response is valid
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != HTTP_STATUS_OK) {
            throw new ApiException("Invalid response from server: " +
                    status.toString());
        }

        // Pull content stream from response
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();

        ByteArrayOutputStream content = new ByteArrayOutputStream();

        // Read response into a buffered stream
        int readBytes = 0;
        while ((readBytes = inputStream.read(sBuffer)) != -1) {
            content.write(sBuffer, 0, readBytes);
        }

        // Return result from buffered stream
        return new String(content.toByteArray());
    } catch (IOException e) {
        throw new ApiException("Problem communicating with API", e);
    }
}
Reporter answered 29/3, 2010 at 16:43 Comment(0)
R
0

See if this little beastie will help you. It may be that you need this in the emulator image you are running.

http://openhandsetmagazine.com/2007/11/tips-howto-connect-android-emulator-behind-proxy/

Rabb answered 29/3, 2010 at 16:48 Comment(2)
I am under windows, I don't even find the /data/data/com.google.android.providers.settings/databases/settings.db location. Where is the file located?Reporter
Ok you need to get into a "shell" in the emulation image. developer.android.com/guide/developing/tools/… That should get you in there for what you need.Rabb
C
5

You can set proxy in your code too.

   public void setProxy(DefaultHttpClient httpclient) {  
           final String PROXY_IP = "<insert your IP here>";  
            final int PROXY_PORT = <insert_PROXY_PORT#>;  

            httpclient.getCredentialsProvider().setCredentials(  
                    new AuthScope(PROXY_IP, PROXY_PORT),  
                    new UsernamePasswordCredentials(  
                            "username", "password"));  

           HttpHost proxy = new HttpHost(PROXY_IP, PROXY_PORT);  

           httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,  
                   proxy);  


       }  
Cort answered 30/3, 2010 at 4:53 Comment(0)
R
0

See if this little beastie will help you. It may be that you need this in the emulator image you are running.

http://openhandsetmagazine.com/2007/11/tips-howto-connect-android-emulator-behind-proxy/

Rabb answered 29/3, 2010 at 16:48 Comment(2)
I am under windows, I don't even find the /data/data/com.google.android.providers.settings/databases/settings.db location. Where is the file located?Reporter
Ok you need to get into a "shell" in the emulation image. developer.android.com/guide/developing/tools/… That should get you in there for what you need.Rabb

© 2022 - 2024 — McMap. All rights reserved.