0

我在做一个简单的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 我将不完整称为被截断的响应?

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

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

在此处输入图像描述

谢谢!

4

5 回答 5

2

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

String responseString = EntityUtils.toString(response.getEntity());
于 2013-09-23T15:06:34.390 回答
1

在过去的几天里,我遇到了完全相同的问题。我发现我的代码可以通过 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 接口。希望有帮助。

于 2013-09-25T13:03:33.560 回答
1

我已经确认这是因为 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 响应创建自定义方法);。

谢谢

阿尔沙德

于 2014-04-23T14:23:46.080 回答
1

您好现在我正在使用 Gson 库来处理响应。

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

谢谢阿尔沙德

于 2015-02-12T14:01:19.083 回答
1

由于声誉,我不能直接发表评论,但在回应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 协议本身的大小限制有时可能是一个因素,但大多数服务器和客户端在内部处理此限制,以免将其暴露给用户。

于 2016-06-08T21:32:27.930 回答