HTTPGET的android不完全反应的BufferedReader

编程入门 行业动态 更新时间:2024-10-23 16:22:29
本文介绍了HTTPGET的android不完全反应的BufferedReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

林做一个简单的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("tiks.document.dev.chocolatecoded.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("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.

更多推荐

HTTPGET的android不完全反应的BufferedReader

本文发布于:2023-11-27 08:57:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不完全   HTTPGET   android   BufferedReader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!