过去一周我一直在搞砸一个问题......
我需要一个接一个地运行在同一个类中定义的方法。他们确实连接到网络服务并向它发送巨大的 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();
}
}
此外,在我使用的每种方法中,都将超过 250 个 JSONObjects 存储在一个 JSONArray 中......当我运行应用程序时,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;
}