HTTPGET的android不完全反应的BufferedReader不完全、反应、HTTPGET、android

2023-09-04 03:33:18 作者:因为我的勇气

林做一个简单的HTTP GET,

Im doing a simple http get,

我对我的结果不完全反应看, 什么即时做错了什么?

I see on my result an incomplete response, what Im doing wrong?

这里的code:

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 我指的是不完整的作为被截断的反应?

edit 1 I refer to incomplete as the response that gets truncated?

请注意下面的反应图像串如何被截断,

please notice in below image of response how string gets truncated,

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

is this because of the log size?, but the other problem is that it doesn't parse?

谢谢!

推荐答案

我已经在过去几天有完全一样的问题。我发现我的code工作通过WiFi而不是3G。换句话说,我消灭了所有常用的螺纹候选人。我还发现,当我跑了code在调试器,只是等待(说)client.execute(...)10秒后,它的工作。

I've had exactly the same issue for the last couple of days. I found that my code worked over WiFi but not 3G. In other words I eliminated all the usual threading candidates. I also found that when I ran the code in the debugger and just waited for (say) 10 seconds after client.execute(...) it worked.

我的猜测是,

response = httpclient.execute(httpget);

本身就是一个异步调用时,它的回报慢部分结果......因此,JSON反序列化出了问题。

is an asynchronous call in itself and when it's slow returns a partial result... hence JSON deserialization goes wrong.

相反,我试过这个版本有回调...

Instead I tried this version of execute with a callback...

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

和突然间所有的工作。如果你不想要一个字符串,或者希望自己的code再看看在ResponseHandler的界面。希望有所帮助。

And suddenly it all works. If you don't want a string, or want your own code then have a look at the ResponseHandler interface. Hope that helps.