0

結果のエンコーディングを特定のエンコーディングに設定するにはどうすればよいですか (特にドイツ語のウムラウト - ä ö ß の場合)

これが私がすることです:

        BasicConnPool pool = this.cluster.getPool();
        HttpProcessor httpproc = this.cluster.getHttpproc();
        HttpRequestExecutor httpexecutor = this.cluster.getHttpExecutor();
        ConnectionReuseStrategy connStrategy = this.cluster.getConnStrategy();
        HttpContext context = new BasicHttpContext();
        URI targetUri;
        try {
            targetUri = new URI("http://www.amazon.de");
        } catch (java.net.URISyntaxException ex) {
            Control.getLogger(this.getClass()).fatal("Big Error in URI");
            return;
        }
        HttpHost targetHost = new HttpHost(targetUri.getHost(), targetUri.getPort() == -1 ? 80 : targetUri.getPort());
        Future<BasicPoolEntry> future = pool.lease(targetHost, null);
        HttpRequest request = new BasicHttpRequest("GET", "http://www.amazon.de");
        boolean reusable = false;
        try {
            BasicPoolEntry entry = future.get();
            try {
                HttpClientConnection conn = entry.getConnection();
                context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
                context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
                httpexecutor.preProcess(request, httpproc, context);
                HttpResponse response = httpexecutor.execute(request, conn, context);
                httpexecutor.postProcess(response, httpproc, context);
                HttpEntity entity = response.getEntity();
                InputStream body = entity.getContent();
                if (connStrategy.keepAlive(response, context)) {
                    reusable = true;
                }
                for (HeaderIterator it = request.headerIterator(); it.hasNext(); ) {
                    it.next();
                    it.remove();
                }
            } finally {
                pool.release(entry, reusable);
            }
        } catch (InterruptedException ex) {
            Control.getLogger(this.getClass()).error(ex.getMessage());
        } catch (ExecutionException ex) {
            Control.getLogger(this.getClass()).error(ex.getMessage());
        } catch (IOException ex) {
            Control.getLogger(this.getClass()).error(ex.getMessage());
        } catch (HttpException ex) {
            Control.getLogger(this.getClass()).error("HttpException in DownloadThread.", ex);
        }

これはほんの一例です。

結果には次のような奇妙な記号が含まれています

 � 1998-2012、Amazon.com, Inc. または Tochtergesellschaften

編集:

これは、私が使用している上記のコードのいくつかの要素の私のコンストラクタです:

        HttpParams params = new SyncBasicHttpParams();
        params.setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false);
        params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024);
        params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 15000);

        this.httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[]{
                new RequestContent(),
                new RequestTargetHost(),
                new RequestConnControl(),
                new RequestUserAgent()
        }, null);
        this.httpexecutor = new HttpRequestExecutor();
        this.connStrategy = new DefaultConnectionReuseStrategy();
        this.pool = new BasicConnPool(params);
        this.pool.setMaxTotal(2000);

前もって感謝します

4

1 に答える 1

0

何てことだ。気にしない :)

これを使用して、入力ストリームを文字列にエンコードしました。

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.body));
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }

            bufferedReader.close();

それで、トリックは何をしましたか?最初の行をこれに置き換えました:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.body, Charset.forName("ISO-8859-1")));

閉じました:)これが同じ問題を抱えている人に役立つことを願っています。

于 2012-03-26T19:00:36.343 に答える