0

インターフェイス Web サイトから JSON コードをダウンロードするための Async-Download-Class を作成しました。実際には動作しますが、時々動作しなくなり、ダウンロード ログに文字列であるはずの疑問符が表示されます。以前にこの問題が発生し、バッファ サイズを増やすだけで解決しましたが、これはアプリの速度に影響します。ご想像のとおり、JSON リーダーはタイムスタンプを検索しますが、タイムスタンプを見つけることができません。したがって、エラーは formatexception ですが、これは関係ないと思います。(場合によっては未定オブジェクトも)

10-17 18:38:11.443: D/debug(13532): {"ident":"50572","ts":"24.05.2011 07:40","content":"<inmydays> Bin ich der einzige der dachte, beim Film 1+1=10 geht es um Nerds und nicht darum, dass 2 Schauspieler die 10 Hauptrollen spielen?","rating":"2044"},
10-17 18:38:11.443: D/debug(13532): {"ident":"28685","ts":"20.12.2009 08:40","content":"<Frau> ich wohne jetzt hier fast 10 monate und habe heute endlich mal meinen nachbarn von nebenan kennengelernt[newline]<Alex> Glückwunsch [newline]<Frau> in der videothek... Hat sich 7 pornos ausgeliehen[newline]<Alex> lol","rating":"4787"},
10-17 18:38:11.443: D/debug(13532): {"ident":"12820�������������������������������������������������������������������������������������������������������������������������������������������������������������

※わかりやすいようにクエスチョンマークのほとんどを削除しました。

@Override
    protected String doInBackground(String... params) {
        String urlStr = params[0];
        InputStream is = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setReadTimeout(100 * 1000);
            connection.setConnectTimeout(100 * 1000);
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.connect();
            int response = connection.getResponseCode();
            Log.d("debug", "The response is: " + response);
            is = connection.getInputStream();

            // read string
            // buffersize:
            // ~max 1024 - 5
            //
            SharedPreferences sharedPref = PreferenceManager
                    .getDefaultSharedPreferences(activity);
            String displayed_items = sharedPref.getString(
                    "pref_key_numberofitems", "10");
            int items = Integer.parseInt(displayed_items);

            if (items > 10) {
                final int bufferSize = 40960 * 2;
                byte[] buffer = new byte[40960 * 2];
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                while (true) {
                    int count = is.read(buffer, 0, bufferSize);
                    if (count == -1) {
                        break;
                    }

                    os.write(buffer);
                }
                os.close();

                result = new String(os.toByteArray(), "iso-8859-1");
                Log.d("debug", result);
            } else {
                final int bufferSize = 2048 * 7;
                byte[] buffer = new byte[2048 * 7];
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                while (true) {
                    int count = is.read(buffer, 0, bufferSize);
                    if (count == -1) {
                        break;
                    }

                    os.write(buffer);
                }
                os.close();

                result = new String(os.toByteArray(), "iso-8859-1");
                Log.d("debug", result);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

編集: インターフェイス Web サイトからデータをダウンロードする別の方法はありますか? 多分これは私を助けるでしょう。

4

2 に答える 2

0

ISO-8859-1 の代わりに UTF-8 のような別のエンコーディングを使用するとどうなりますか?

于 2013-10-17T17:37:11.103 に答える