我的應用程序是移動購物助手。我已經嘗試了一些代碼,但它不工作。我想從PHP發送數據到Android。每次在列表中添加一個新項目(在php中),它都必須在Android中進行同步。我的示例代碼:如何使用網絡服務在Android中進行同步
public void onClick(View arg0) {
tbnum=txtproduct.getText().toString();
final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("id", mid);
request.addProperty("tbnum", tbnum);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug=true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
System.out.println("response: " + androidHttpTransport.responseDump);
SoapObject result = (SoapObject)envelope.getResponse();
//SoapObject result = (SoapObject) envelope.getResponse();
if(result!=null){
//String strRes = result.toString();
Toast.makeText(getApplicationContext(),"item has been received", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(),
"Web Service not Response!", Toast.LENGTH_LONG).show();
}
如果你想即時更新,然後你就可以應該看看gcm push notification.http://developer.android.com/google/gcm/index.html如果你希望你的應用程序以特定的時間間隔更新到服務器,那麼你將不得不創建一個服務。 http://developer.android.com/training/sync-adapters/creating-sync-adapter.html – playmaker420