Question

La getUrlContent norme fonctionne welll lorsqu'il n'y a pas de pare-feu. Mais je suis des exceptions lorsque je tente de le faire derrière un pare-feu.

J'ai essayé de mettre « http serveur proxy » dans le gestionnaire AVD, mais cela n'a pas fonctionné. Toute idée comment configurer correctement en place?

et BTW: de la documentation android "Vous pouvez utiliser l'option verbose-proxy pour diagnostiquer les problèmes de connexion proxy" verbose-proxy n'est pas une option valable.

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);
    }
}
Était-ce utile?

La solution

Voyez si cette petite bestiole vous aidera. Il se peut que vous en avez besoin dans l'image de l'émulateur que vous utilisez.

http://openhandsetmagazine.com/ 2007/11 / Conseils-howto-connect-android-émulateur-derrière-proxy /

Autres conseils

Vous pouvez définir proxy dans votre code aussi.

   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);  


       }  
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top