I've been messing with a prob for past week over now ...
I need to run method defined in same class just after each other. They do connect to web-service and send huge JSON to it ...
What I'm doin' is ...
public class something extends service {
// Run method which are too defined in the same class
public void onStartCommand() {
run();
}
public void run() {
method1();
method2();
method3();
method4();
method5();
}
}
Also, in each method I'm using is storing over 250 JSONObjects in a single JSONArray ... When I run the app, only a few objects of the array in the first method are read by the php script on web, later that it breaks! I can't figure out what it is the prob!
the php on the server to handle it is ...
<?php
function write_contact($imei,$name,$phone) {
$mysql = "config/mysql.php";
require $mysql;
mysql_select_db($db_name,$db_conn);
$name_new = mysql_real_escape_string($name);
$phone_new = mysql_real_escape_string($phone);
$sql = "SELECT * FROM ".$imei."_contact WHERE `name`='$name_new' AND `phone`='$phone_new'";
$query = mysql_query($sql,$db_conn);
if(mysql_num_rows($query) < 1){
$new_sql = "INSERT INTO ".$imei."_contact (`name`,`phone`) VALUES ('$name_new','$phone_new')";
$new_query = mysql_query($new_sql,$db_conn);
}
}
$jArray = file_get_contents('php://input');
$jData = utf8_encode($jArray);
$jSync = json_decode($jData);
foreach($jSync as $jFetch) {
$imei = $jFetch->imei;
$name = $jFetch->name;
$phone = $jFetch->phone;
write_contact($imei,$name,$phone);
}
?>
One of the Android method i'm executing ...
public void syncContact() {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
JSONArray jContact = new JSONArray();
String imei = getIMEI();
while (phones.moveToNext())
{
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
JSONObject contactSync = new JSONObject();
try {
contactSync.put("imei", imei);
contactSync.put("name", name);
contactSync.put("phone", phone);
} catch (JSONException ex) {
Log.i("Error",ex.getMessage());
}
jContact.put(contactSync);
}
postSync("contactSync.php",jContact);
phones.close();
}
//Post Method
public HttpResponse postSync(String url, JSONObject jObject) {
HttpClient client = new DefaultHttpClient();
String base_url = "http://www.myurl.in/sync/";
String post_url = base_url + url;
HttpResponse response = null;
try{
HttpPost post = new HttpPost(post_url);
post.setHeader("JSON",jObject.toString());
StringEntity se = new StringEntity(jObject.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
Log.i("HTTP JSON",jObject.toString());
}
catch(Exception ex){
Log.e("Error",ex.getMessage());
}
int status = response.getStatusLine().getStatusCode();
System.out.println("HTTP post status = " + status);
return response;
}