
我正在尝试从Web加载大量数据到我的Android应用程序,我一直收到这个错误:
07-18 10:16:00.575: E/AndroIDRuntime(30117): java.lang.OutOfMemoryError: [memory exhausted]并且已经阅读了很多关于JsON的内容.我找到了一些解决方案,但没有什么真正帮助我.
这是我的代码:
public class HistoricoAdapter extends BaseAdapter { private Context ctx; JsONArray JsonArray; public HistoricoAdapter(Context ctx) { this.ctx = ctx; String readhttp = readhttp(); try { // transforma a string retornada pela função readhttp() em array JsonArray = new JsONArray(readhttp); } catch (Exception e) { e.printstacktrace(); } } public String readhttp() { // Acessa a URL que retorna uma string com os dados do banco StringBuilder builder = new StringBuilder(); httpClIEnt clIEnt = new DefaulthttpClIEnt(); httpGet httpGet = new httpGet("some url"); try { httpResponse response = clIEnt.execute(httpGet); Statusline statusline = response.getStatusline(); int statusCode = statusline.getStatusCode(); if (statusCode == 200) { httpentity entity = response.getEntity(); inputStream content = entity.getContent(); BufferedReader reader = new BufferedReader( new inputStreamReader(content)); String line; while ((line = reader.readline()) != null) { builder.append(line); } } else { Log.e(this.toString(), "Erro ao ler JsON!"); } } catch (ClIEntProtocolException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } return builder.toString(); } public int getCount() { return JsonArray.length(); } public boolean isEmpty(){ if(JsonArray.toString().isEmpty()){ return true; } else { return false; } } public Object getItem(int position) { JsONObject ob = null; try { ob = JsonArray.getJsONObject(position); } catch (JsONException e) { // Todo auto-generated catch block e.printstacktrace(); } return ob; } public long getItemID(int arg0) { return 0; } public VIEw getVIEw(int position, VIEw vIEw, VIEwGroup arg2) { LayoutInflater layout = (LayoutInflater) ctx .getSystemService(Context.LAYOUT_INFLATER_SERVICE); VIEw v = layout.inflate(R.layout.Listar_compromisso, null); try { JsONObject obj = (JsONObject) getItem(position); } catch (Exception ex) { } return v; }}任何人都可以预测为什么我会收到此错误?
解决方法:
如果出现此错误,那么您的JsON必须太大而无法缓冲到内存中.
问题是org.json太基本无法处理.
您需要一个高级库来传输响应,例如GSON或Jackson.
> GSON – Streaming
> Jackson – Processing model: Streaming API
以上是内存溢出为你收集整理的java – JSON和内存问题全部内容,希望文章能够帮你解决java – JSON和内存问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)