在Android服务类中运行同步方法

时间:2012-11-05 16:17:27

标签: php android json http sync

过去一周我一直在搞乱一个问题......

我需要在彼此之后运行在同一个类中定义的方法。他们确实连接到Web服务并向它发送巨大的JSON ......

我做的是......

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();
}
}

此外,在我使用的每个方法中,在一个JSONArray中存储超过250个JSONObject ... 当我运行应用程序时,第一个方法中只有几个数组对象被web上的php脚本读取,之后就会中断!我无法弄清楚这是什么问题!

服务器上的php处理它是......

<?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);
    }
?>

我正在执行的Android方法之一...

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;
    }

1 个答案:

答案 0 :(得分:0)

看起来你想做线程,这是正确的方法,但你没有正确地做到这一点。实现run()方法只是其中的一部分。一个对象很好地实现了runnable接口,它定义了你拥有的run()方法。然后必须将runnable对象传递给Thread构造函数,并且必须通过start()启动生成的Thread对象。您还可以定义Thread类并在其中实现run方法。所以有几种方法可以做到这一点,但在你的情况下,最简单的可能看起来像这样:

onStartCommand()
{
   new Thread()
   {
     public void run()
     {
        method1();
     }
   }.start();

   new Thread()
   {
     public void run()
     {
        method2();
     }
   }.start();
  ...
}

等等。这样做是定义一个匿名内部类(new Thread(){})并在该匿名内部类上的新Thread()构造函数实例化的对象上调用start()。这些匿名内部类中的每一个都会产生自己的执行线程,这将导致JVM在每个中执行run()方法。然后run方法将调用你的method1(),method2()等“worker”方法,并且每个方法都会执行“同时”