0

私はこの質問によって私の長期的な問題を解決したいと思います、そしてあなたたちが助けてくれることを願っています、しかし最初に; HTTPS自己署名証明書サーバーへの接続に問題が発生して3週間近くになります。ここに複数の解決策があるにもかかわらず、私は自分の問題を解決できないようです。おそらく私はそれを正しく使用する方法を知らなかったか、いくつかのファイルを持っていなかったか、正しいライブラリをインポートしていませんでした。

接続しようとしているhttpsサイトから証明書をダウンロードする必要があるWebサイトに出くわしましたが、そのときに接続しました。作成した証明書またはキーストアを使用する前に、いくつかの手順を実行する必要があります。私はこのウェブサイトからこの解決策を得ました:

Android:SSL証明書を信頼する

// Instantiate the custom HttpClient
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23");
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();

上記のように、最後の行の後に問題があります。responseEntityで何をしますか?https WebサイトをWebViewに表示したい場合、どのように使用しますか?いくつかの助けと説明がいいでしょう:)

4

3 に答える 3

4

正しい方法でコンテンツが必要な場合はHttpEntity、ストリームを取得するための呼び出しや、AndroidSDKですでに利用可能な無意味な作業を大量に行うことは含まれません。HttpEntity#getContent()

代わりにこれを試してください。

// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();

// Retrieve a String from the response entity
String content = EntityUtils.toString(responseEntity);

// Now content will contain whatever the server responded with and you
// can pass it to your WebView using #loadDataWithBaseURL

表示するときにWebView#loadDataWithBaseURLを使用することを検討してくださいcontent

于 2012-03-13T09:34:29.147 に答える
0

要求されたURLに対してInputStreamresponseEntity.getContent()で応答を取得するには、を呼び出す必要があります。そのストリームを使用して、必要に応じてデータを表示します。たとえば、期待されるデータが文字列である場合、次の方法でこのストリームを文字列に変換できます。

/**
 * Converts InputStream to String and closes the stream afterwards
 * @param is Stream which needs to be converted to string
 * @return String value out form stream or NULL if stream is null or invalid.
 * Finally the stream is closed too. 
 */
public static String streamToString(InputStream is) {
    try {
        StringBuilder sb = new StringBuilder();
        BufferedReader tmp = new BufferedReader(new InputStreamReader(is),65728);
        String line = null;

        while ((line = tmp.readLine()) != null) {
            sb.append(line);
        }

        //close stream
        is.close();

        return sb.toString();
    }
    catch (IOException e) { e.printStackTrace(); }
    catch (Exception e) { e.printStackTrace(); }

    return null;
}
于 2012-03-13T07:22:36.513 に答える
0
InputStream is = responseEntity.getContent();
 try{
 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
         sb.append(reader.readLine() + "\n");
         String line="0";
         while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
          }

     String   result=sb.toString();
          is.close();
 }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
          }

文字列「結果」にすべてのコンテンツが含まれます

于 2012-03-13T07:26:30.113 に答える