2013-03-14 83 views
0

下面的代碼每30秒產生7個隨機數字。如何同步我的應用程序和服務器之間的時間?

public class MainActivity extends Activity { 

Timer timeoutTimer; 
final Random myRandom = new Random(); 
GenerateTask genTask = new GenerateTask(); 
static String RAN_TEXT = "text"; 

class GenerateTask extends TimerTask { 
    boolean started = false; 
    public void run() { 
     if (started) { 
      System.out.println("generating"); 
      final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber); 



      RAN_TEXT = ""; 

      for(int k=0;k<7;k++){ 
       RAN_TEXT += myRandom.nextInt(10) + " "; 
      } 
      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        textGenerateNumber.setText(RAN_TEXT); 
       } 
      }); 
     } 
    } 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 




    Button buttonGenerate = (Button)findViewById(R.id.generateme); 


    buttonGenerate.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      System.out.println("click"); 
      if (!genTask.started) { 
       genTask.started=true; 
       timeoutTimer = new Timer(); 
       timeoutTimer.scheduleAtFixedRate(genTask, 0, 30000); 
      } else { 
       genTask.started=false; 
       timeoutTimer.cancel(); 
      } 
     } 
    }); 
} 
} 

我將在申請時間同步到服務器:

http://server10.instaforex.com:2012/TimeService/TimeService.svc?wsdl 

如何同步服務器時我的應用程序?

回答

0

對於您必須使用HTTP連接

請參見下面的代碼

public long getCurrentTimeFromServer() { 

    long serverTime = 0; 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet(
      "your url"); 
    HttpResponse response; 
    try { 
     response = httpclient.execute(httpget); 
     // Examine the response status 
     // Get hold of the response entity 
     HttpEntity entity = response.getEntity(); 
     // If the response does not enclose an entity, there is no need 
     // to worry about connection release 

     if (entity != null) { 

      // A Simple JSON Response Read 
      InputStream instream = entity.getContent(); 
      String result = Utilities.convertinputStreamToString(instream); 
      serverTime = Long.parseLong(result); 
      // now you have the string representation of the HTML request 
      instream.close(); 
     } 

    } catch (Exception e) { 
     Log.e("tag", "error", e); 
    } 

    return serverTime; 
} 
+0

我需要爲該代碼創建另一個java類嗎?併爲URL我只是粘貼這個(http://server10.instaforex.com:2012/TimeService/TimeService.svc?wsdl)的權利? – jun 2013-03-14 09:49:36

+0

我想你將需要創建web服務類型哪個返回服務器時間 – Nirali 2013-03-14 09:51:26

+0

嗯好吧,但我不知道該怎麼做?那麼上面的代碼呢? – jun 2013-03-14 10:02:57

0

爲了acheave你的目標,你需要首先從服務器獲取正確的時間。我在服務器端播種,你有wsdl,所以在你的客戶端(你的應用),你需要創建一個連接到服務器並獲得時間的進程。

關於如何做到這一點,點擊這裏:http://www.ibm.com/developerworks/webservices/library/ws-android/index.html

之後,你就會有正確的時間在本地的應用程序,你,你就可以做任何與時間。

+0

你可以舉例說明我的代碼嗎? – jun 2013-03-14 10:26:32

相關問題