【问题标题】:Transfering Base64 String over Http REST service with JSON response使用 JSON 响应通过 Http REST 服务传输 Base64 字符串
【发布时间】:2013-08-26 06:40:09
【问题描述】:

我有一个返回 json 响应的 web 服务,json 响应包含纯文本和 base64 编码的图像,我正在使用 android 应用程序使用该服务,所以我实现了进度条来指示进度。

实现进度条迫使我使用 BufferedInputStream 读取响应并根据应用正在读取的内容更新进度。

问题是一切正常,进度更新正确,但是在收集响应并退出 while 循环后,我尝试使用 JSONObject 将字符串转换为 json 格式。 这是代码sn -p

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

            }

请帮忙

【问题讨论】:

    标签: java android json http base64


    【解决方案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();
    
        }
    

    【讨论】:

    • 谢谢!!其实我之前试过htis,但问题是,这段代码会把响应读成一行,所以进度会直接从0到100%
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2011-06-30
    • 2023-04-11
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多