我想一次向PHP服務器發送多個Json字符串[每個1 MB]。當我試圖發送10條記錄時,它的工作正常。但是,如果它超過了20條記錄,則表示OutOfMemoryException
。我看到,Android內存大小限制t0 15MB - 16MB。但沒有任何線索如何解決這個問題,我正在使用下面的代碼一次發送多個記錄。嘗試向Android中的服務器發送大型json數據時發生OutOfMemoryException?
/** Upload Data*/
private void submitUploadData(String url, Map<String, String> param)
throws IOException {
URL siteUrl;
try {
siteUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) siteUrl
.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
String content1 = "";
Set getkey = param.keySet();
Iterator keyIter = getkey.iterator();
String content = "";
for (int i = 0; keyIter.hasNext(); i++) {
Object key = keyIter.next();
if (i != 0) {
content += "&"; //Crashing here
}
content += key + "=" + param.get(key); //Crashing here
System.out.println("Content" + content);
}
System.out.println(content);
out.writeBytes(content.trim());
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String jsonresponse = "";
while ((jsonresponse = in.readLine()) != null) {
System.out.println(jsonresponse);
if(!jsonresponse.equals("Authorisation Successful")){
updateUploadRecords(jsonresponse);}
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
這可能是因爲你的數據集太大了。一些快速評論 - 1)您似乎正在創建一個查詢字符串,請記住,大多數代理將限制查詢字符串的長度。 2)你正在用可能最糟糕的方式構建你的'內容'字符串。 – Perception 2013-03-06 09:46:31
使用一個stringbuilder來構建你的內容(或者keyvalue對,或者textutils.join ...) – njzk2 2013-03-06 09:47:41
@perception什麼纔是構建內容的正確方式? – GoCrazy 2013-03-06 09:52:59