0

私はjson応答を返すWebサービスを持っています.json応答にはプレーンテキストとbase64でエンコードされた画像の両方が含まれています.Androidアプリを使用してそのサービスを消費しているので、進行状況を示す進行状況バーを実装しました.

進行状況バーを実装すると、BufferedInputStream を使用して応答を読み取り、アプリの読み取り内容に基づいて進行状況を更新する必要があります。

問題は、すべてが正常に機能し、進行状況が正しく更新されていることですが、応答を収集して while ループを終了した後、JSONObject を使用して文字列を json 形式に変換しようとしています。ここにコードスニペットがあります

BufferedInputStream bis = new BufferedInputStream(responseEntity.getContent());
            StringBuilder sb = new StringBuilder();
            String line = null;
            int total = 0 ;
            int count = 0 ;
            byte[] buffer = new byte[4096];
            StringBuffer sBuffer = new StringBuffer();
            StringWriter sw = new StringWriter();
            String content = new String();

            while((count = bis.read(buffer)) > 0){
                content += new String(buffer,Charset.defaultCharset());

                total += count;
                publishProgress(""+(int )total*100/this.contentSize);
                Log.i("updating",""+(int )total*100/this.contentSize);
            }


            bis.close();
           // String content = new String(sb);
            // Log.i("ServerRawresponse",content);
            try {
                Log.i("REsponse_Content",content.replaceAll("\"", ""));
                responseString = new JSONObject(new JSONTokener(content.replaceAll("\"", "\\\"")));
                //System.out.println(content);
            } catch (JSONException e) {
                e.printStackTrace();

            }

助けてください

4

1 に答える 1

1

この方法を試してみてください

HttpResponse WSresponse = httpclient.execute(httppost);
String response = getResponseBody(WSresponse.getEntity());
JSONObject jobj = new JSONObject(response);

public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {

        System.out.println("GEN START : " + Calendar.getInstance().getTimeInMillis());
        if (entity == null) {
            throw new IllegalArgumentException("HTTP entity may not be null");
        }

        InputStream instream = entity.getContent();

        if (instream == null) {
            return "";
        }

        if (entity.getContentLength() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(

            "HTTP entity too large to be buffered in memory");
        }


        StringBuilder buffer = new StringBuilder();

        BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

        } finally {
            instream.close();
            reader.close();
        }

        return buffer.toString();

    }
于 2013-08-26T06:56:00.780 に答える