I have created an android application for off line concept. when internet connected data can be sync through server.
My tablet pc has more than 5.7GB storage space as internal memory. when I sync data from the server, its reporting an error as out of memory.
I think data storage location is changed, its storing some where else. Can anyone resolve this problem? I think some modification to be made in manifest file.
My sync code
public class sync extends Activity implements OnClickListener {
private static String url = "Json URL";
// Button
Button chktosync, logout_menu;
private ProgressDialog pd;
DBAdapter db = new DBAdapter(this);
JSONArray contacts = null;
JSONArray cropdetails = null;
JSONArray biodetails = null;
String land, area, sf, land_id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sync);
logout_menu = (Button) findViewById(R.id.logout_menu);
logout_menu.setOnClickListener(this);
chktosync = (Button) findViewById(R.id.chktosync);
chktosync.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pd = ProgressDialog.show(sync.this, "Synchronizing", "Synchronizing...");
new Thread() {
public void run() {
try {
processThread();
pestanddisease();
bioparam();
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
pd.dismiss();
}
}.start();
}
});
}
private void processThread() {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(loginPage.code);
db.open();
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String farmerid = c.getString("farmerid");
String farmername = c.getString("farmername");
String farmer_fathername = c.getString("farmer_fathername");
String farmer_mobilenumber = c.getString("farmer_mobilenumber");
String districtname = c.getString("districtname");
String blockname = c.getString("blockname");
String villagename = c.getString("villagename");
db.insertFarmer(farmerid, farmername,
farmer_fathername, farmer_mobilenumber,
districtname, blockname, villagename);
//land details
JSONArray Land = c.getJSONArray("land_details");
for (int l = 0; l < Land.length(); l++) {
JSONObject s = Land.getJSONObject(l);
String survey_no = s.getString("survey_no");
String area = s.getString("area");
String land_type = s.getString("land_type");
String patternref = s.getString("patternref");
String crop_matrix_id = s.getString("crop_matrix");
land_id = s.getString("land_id");
String season1_crop = s.getString("season1_crop");
String season2_crop = s.getString("season2_crop");
String season3_crop = s.getString("season3_crop");
db.insertlanddetails(farmerid, survey_no, area, land_type, patternref, land_id, crop_matrix_id, season1_crop, season2_crop, season3_crop);
}
db.close();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
public void pestanddisease()
{
/// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(pestdisease);
try
{
// Getting Array of Contacts
cropdetails = json.getJSONArray("CropDetails");
db.open();
for (int i = 0; i < cropdetails.length(); i++) {
JSONObject s = cropdetails.getJSONObject(i);
String cropname = s.getString("crop");
String pest = s.getString("pest_name");
String disease = s.getString("disease_name");
db.insertcrop_pest_details(cropname, pest);
db.insertcrop_disease_details(cropname, disease);
}
db.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
JSONParser code
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),1048576);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
line=reader.readLine();
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Log Cat
08-28 02:06:59.199: E/dalvikvm-heap(304): Out of memory on a 9513056-byte allocation.
08-28 02:06:59.446: E/AndroidRuntime(304): Uncaught handler: thread Thread-9 exiting due to uncaught exception
08-28 02:06:59.732: E/AndroidRuntime(304): java.lang.OutOfMemoryError
08-28 02:06:59.732: E/AndroidRuntime(304): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97)
08-28 02:06:59.732: E/AndroidRuntime(304): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:131)
08-28 02:06:59.732: E/AndroidRuntime(304): at java.lang.StringBuilder.append(StringBuilder.java:271)
08-28 02:06:59.732: E/AndroidRuntime(304): at java.io.BufferedReader.readLine(BufferedReader.java:413)
08-28 02:06:59.732: E/AndroidRuntime(304): at com.login.JSONParser.getJSONFromUrl(JSONParser.java:55)
08-28 02:06:59.732: E/AndroidRuntime(304): at com.login.sync.processThread(sync.java:76)
08-28 02:06:59.732: E/AndroidRuntime(304): at com.login.sync.access$1(sync.java:70)
08-28 02:06:59.732: E/AndroidRuntime(304): at com.login.sync$1$1.run(sync.java:57)
08-28 02:07:00.416: I/dalvikvm(304): threadid=7: reacting to signal 3
08-28 02:07:00.416: E/dalvikvm(304): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
Json parser working fine. No need to change it into file. Reason for the error is JSON was not coming properly.I did some modification on server side. Now its working fine. Before synchronizing data please ensure that JSON is valid or not.Thanks for your support…