1

標準の getUrlContent は、ファイアウォールがない場合にうまく機能します。しかし、ファイアウォールの内側で実行しようとすると例外が発生します。

AVD マネージャーで「http プロキシ サーバー」を設定しようとしましたが、うまくいきませんでした。正しく設定する方法はありますか?

ところで:Androidのドキュメントから「-verbose-proxyオプションを使用して、プロキシ接続の問題を診断できます。」-verbose-proxy は有効なオプションではありません。

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

2 に答える 2

5

コードにプロキシを設定することもできます。

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


       }  
于 2010-03-30T04:53:04.150 に答える
0

この小さな獣があなたを助けるかどうか見てください。実行しているエミュレータイメージでこれが必要な場合があります。

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

于 2010-03-29T16:48:12.693 に答える