2015-10-08 51 views
0

我想获得一个JSON答案类型,但到大,在50 MB的Android工作室抛出新的异常内存不足内存不足JSON的Android

class MyClass extends AsyncTask<Void, Void, Void> 
{ 
String result=""; 

@Override 
protected Void doInBackground(Void... params){ 

HttpClient httpClient=new DefaultHttpClient(); 
String URL="http://82.79.121.114:1001/api/search/category/3,1,1"; 

try{ 

HttpGet httpGet = new HttpGet(URL); 

httpGet.setHeader("Authorization", "Bearer " + AccesToken); 

HttpResponse httpResponse=httpClient.execute(httpGet); 
//Log.e("EROARE!!!!!!","EROARE!!!!!"); 

HttpEntity httpEntity=httpResponse.getEntity(); 

InputStream is=httpEntity.getContent(); 

result=convert(is); 

}catch (Exception e){ 

e.printStackTrace(); 
} 
return null; 
} 

@Override 
protected void onPostExecute(Void aVoid){ 

Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show(); 

if(result.length() == 0 || result == null) 
{ 
Toast.makeText(MainActivity.this,result.Toast.LENGTH_SHORT).show(); 

if(result.length() == 0 || result == null) 
{ 
Toast.makeText(MainActivity.this,"Nu merge!!!",Toast.LENGTH_SHORT).show(); 
} 
} 

public String convert(InputStream is) throws IOException { 
BufferedReader reader = null; 
StringBuffer buffer = new StringBuffer(); 
try { 

reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8192); 
int read; 
char[] chars = new char[1024]; 
while ((read=reader.read(chars)) != -1) 
buffer.append(chars, 0, read); 

} 
finally { 
if (reader != null) 
reader.close(); 
} 
return buffer.toString(); 
} 
+0

世界上什么使50mb JSONs – poss

回答

0

你的JSON对象实在太大,因为大部分设备没有这么大的堆。如果您拥有服务器端,您应该更改发送给客户端的响应,并将其作为单独响应进行处理。

此外,我建议您重新考虑为什么您一次发送如此多的数据。即使在平均网络连接上加载也需要很长时间。

0

如果确实OutOfMemory问题与您的REST服务返回的数据大小有关,那么您在服务器端和客户端都会出错。一个移动应用程序应该关心用户的数据流量,并关心他们的电池,所以不是一次性加载整个JSON,也许你可以将它拆分成页面,并且只加载第一页。一旦用户感兴趣的更多(也许你正在使用一个ListView来显示这些类别),然后你加载下一页等等。请参阅Android的无尽列表模式这里: https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews