【问题标题】:android HttpGet incomplete response BufferedReaderandroid HttpGet不完整的响应BufferedReader
【发布时间】:2013-09-23 13:19:33
【问题描述】:

我正在做一个简单的http get,

我在结果中看到不完整的回复, 我做错了什么?

这里是代码:

class GetDocuments extends AsyncTask<URL, Void, Void> {

    @Override
    protected Void doInBackground(URL... urls) {

        Log.d("mensa", "bajando");

             //place proper url 
            connect(urls);

            return null;
    }

    public static void connect(URL[] urls)
    {

        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet("http://tiks.document.dev.chocolatecoded.com.au/documents/api/get?type=tree"); 

        // Execute the request
        HttpResponse response;
        try {

            response = httpclient.execute(httpget);



            // Examine the response status
            Log.d("mensa",response.getStatusLine().toString());

            // Get hold of the response entity
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release

            if (entity != null) {

                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                // now you have the string representation of the HTML request

                Log.d("mensa", "estratagema :: "+result);

                JSONObject jObject = new JSONObject(result);

                Log.d("mensa", "resposta jObject::"+jObject);

                Log.d("mensa", "alive 1");

                JSONArray contacts = null;
               contacts = jObject.getJSONArray("success");

               Log.d("mensa", "resposta jObject::"+contacts);

               Log.d("mensa", "alive");

               //instream.close();

            }


        } catch (Exception e) {}
    }



        private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
                Log.d("mensa", "linea ::"+line);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

我称之为:

GetDocuments get = new GetDocuments();

       URL url = null;
    try {
        url = new URL("ftp://mirror.csclub.uwaterloo.ca/index.html");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    //URL url = new URL("http://www.google.es");

    get.execute(url);

编辑 1 我将不完整称为被截断的响应?

请注意下面的响应图像中字符串是如何被截断的,

这是因为日志大小的原因吗? 但另一个问题是它不解析?

谢谢!

【问题讨论】:

  • 您能否详细说明您的 INCOMPLETE 响应。我尝试调试您的代码并抛出 JSONException。 09-23 14:04:42.045: E/a(507): org.json.JSONException: java.lang.Boolean 类型成功时的值 true 无法转换为 JSONArray
  • 我没有遇到异常,请参阅编辑以检查截断的字符串以获取响应,tnx
  • 哦,我得到了例外,因为我对您的代码进行了一些编辑。在您的 GetDocuments 类连接方法上,将日志添加到您的异常中,然后您将看到它
  • 你说得对,谢谢,现在检查一下
  • 我认为您应该在 getJSONArray 上将“成功”更改为“结果”。因为你得到的数组数据不是布尔值。

标签: android http-get


【解决方案1】:

我不知道这是否能解决你的问题,但你可以摆脱你的方法并简单地使用:

String responseString = EntityUtils.toString(response.getEntity());

【讨论】:

    【解决方案2】:

    过去几天我遇到了完全相同的问题。我发现我的代码可以通过 WiFi 而不是 3G。换句话说,我消除了所有常见的线程候选者。我还发现,当我在调试器中运行代码并在 client.execute(...) 之后等待(比如说)10 秒,它就起作用了。

    我的猜测是

    response = httpclient.execute(httpget);
    

    本身是一个异步调用,当它很慢时返回部分结果......因此 JSON 反序列化出错了。

    相反,我尝试使用回调执行此版本...

    try {
        BasicResponseHandler responseHandler = new BasicResponseHandler();
        String json = httpclient.execute(httpget, responseHandler);         
    } finally {
        httpclient.close();
    }
    

    突然间,一切都奏效了。如果您不想要字符串,或者想要自己的代码,请查看 ResponseHandler 接口。希望有帮助。

    【讨论】:

      【解决方案3】:

      我已经确认这是因为 java 字符串的大小限制。我通过在响应中添加字符串“abcd”来检查这一点,并在 logcat 中打印响应字符串。但结果是没有添加字符串“abcd”的截断响应。

      那是

      try {
      BasicResponseHandler responseHandler = new BasicResponseHandler();
      String json = httpclient.execute(httpget, responseHandler);  
      json= json+"abcd";
       Log.d("Json ResponseString", json);
      } finally {
      httpclient.close();
      }
      

      所以我放了一个 arrayString 来收集响应。为了制作数组,我使用“}”拆分了我的 json 格式响应

      代码如下(这只是一种解决方法)

         BasicResponseHandler responseHandler = new BasicResponseHandler();
         String[] array=client.execute(request, responseHandler).split("}");
      

      然后您可以使用自定义类将每个对象解析为 json 对象和 json 数组。

      如果您有任何其他好的方法来存储响应,请分享,因为我正在为每个不同的 json 响应创建自定义方法);。

      谢谢

      阿尔沙德

      【讨论】:

        【解决方案4】:

        嗨,现在我正在使用 Gson 库来处理响应。

        http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

        谢谢 阿尔沙德

        【讨论】:

          【解决方案5】:

          由于声誉,我不能直接发表评论,但在回复 https://stackoverflow.com/a/23247290/4830567 时,我觉得我应该指出 Java 字符串的大小限制约为 2GB (Integer.MAX_VALUE),所以这不是导致此处截断。

          根据https://groups.google.com/d/msg/android-developers/g4YkmrFST6A/z8K3vSdgwEkJ,logcat 有大小限制,这就是为什么在 logcat 中附加“abcd”和打印不起作用的原因。字符串本身会有附加的字符。之前链接的讨论还提到,HTTP 协议本身的大小限制有时可能是一个因素,但大多数服务器和客户端在内部处理此限制,以免将其暴露给用户。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-05-05
            • 2016-05-24
            • 1970-01-01
            • 2012-01-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多