2013-06-25 127 views
1

我在項目中需要使用.NET網站爲android應用程序配置主題。 我擁有的選項是實現從Android應用程序到服務器的輪詢服務,該服務器頻繁輪詢以查看是否需要進行任何更改。 任何一點可以任何更好的方式或方法發送數據從網站到Android應用程序,而不是應用程序頻繁輪詢網站服務器如何將數據從網站服務器發送到android應用程序

+0

輪詢發生了什麼問題?我在你的應用程序中儘量避免使用(使用網絡.NET服務和ksoap) –

+0

你需要什麼類型的輪詢輪詢你的位置? –

+0

@Seraphim輪詢並不是考慮某些事情時的最佳選擇,例如發生更改時從服務器發送到客戶端的請求。 – iJade

回答

3

更好但更復雜的方法是使用Google Cloud Messaging(又名推送通知) 。

這樣你的服務器可以通知應用程序有新的數據要檢索,只有你的應用程序必須查詢你的服務器。

這是一個更加電池友好的方法,工作得很好。我之前使用過這個也是出於同樣的原因。

回答一些太的意見,投票是一個糟糕的主意,因爲

  • 它過度使用這兩種服務器和用戶的設備沒有任何理由
  • 它會消耗用戶的電池
  • 會有當服務器想要與應用程序進行通信時,以及您的應用程序執行下一輪詢的時間總是有些延遲。

推送通知方法需要更多的努力,但也有很大的優勢。

0

嗨在互聯網上有很多這個教程。但無論如何,我張貼代碼來演示如何從android調用Web服務...此代碼僅調用SOAP Web服務。調用其他Web服務,如JSON,REST等在網上搜索自己。

public class HelloWebService extends Activity{ 

String SOAP_ACTION="http://tempuri.org/HelloWorld"; 
String METHOD_NAME = "HelloWorld"; 
String NAMESPACE = "http://tempuri.org/"; 
String URL = "http://192.168.1.15:80/himanshu/helloworldwebservice.asmx"; 
String SUM_SOAP_ACTION="http://tempuri.org/AddNumbers"; 
String METHOD_NAME1 = "AddNumbers"; 

TextView tv1,tv2,tv3,tv4,tv5; 
EditText etA,etB,etName; 
Button bt,dis; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.hello); 

    etName = (EditText)findViewById(R.id.et); 
    tv1 = (TextView)findViewById(R.id.tv1); 
    tv2 = (TextView)findViewById(R.id.tv2); 
    tv3 = (TextView)findViewById(R.id.tv3); 
    tv4 = (TextView)findViewById(R.id.tv4); 
    tv5 = (TextView)findViewById(R.id.tv5); 
    etA = (EditText)findViewById(R.id.editA); 
    etB = (EditText)findViewById(R.id.editB); 
    bt = (Button)findViewById(R.id.add); 
    dis = (Button)findViewById(R.id.display); 

    bt.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      sum(); 
     } 
    }); 

    dis.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Hello();  
     } 
    }); 

} 

public void Hello(){ 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    Log.d("request", request.toString()); 

    String str = etName.getText().toString(); 
    Log.d("str", str); 

    request.addProperty("name", str); 
    Log.d("request", request.toString()); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    Log.d("envelope", envelope.toString()); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 
    Log.d("envelope", envelope.toString()); 
    HttpTransportSE aht = new HttpTransportSE(URL); 
    aht.debug=true; 
    Log.d("aht", aht.toString()); 

    try 
    { 
     aht.call(SOAP_ACTION, envelope); 
     SoapPrimitive results = (SoapPrimitive)envelope.getResponse(); 
     Log.d("result", results.toString()); 
     tv1.setText(""+results.toString()); 
    } 
    catch (Exception e) 
    { 
     tv2.setText(e.getClass().toString()); 
     Log.d("Error",e.getClass().toString()); 
    } 

} 

public void sum(){ 

     SoapObject sum_request = new SoapObject(NAMESPACE, METHOD_NAME1); 
     Log.d("sum_request", sum_request.toString()); 

     //PropertyInfo pro1 = new PropertyInfo(); 
     String strA = etA.getText().toString(); 
     String strB = etB.getText().toString(); 
     sum_request.addProperty("a", strA); 
     sum_request.addProperty("b", strB); 

     Log.d("sum_request", sum_request.toString()); 

     SoapSerializationEnvelope sum_envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     Log.d("sum_envelope", sum_envelope.toString()); 

     sum_envelope.dotNet = true; 
     sum_envelope.setOutputSoapObject(sum_request); 
     Log.d("sum_envelope", sum_envelope.toString()); 

     HttpTransportSE sum_aht = new HttpTransportSE(URL); 
     sum_aht.debug=true; 
     Log.d("sum_aht", sum_aht.toString()); 

     try 
     { 
      sum_aht.call(SUM_SOAP_ACTION, sum_envelope); 
      SoapPrimitive sum_results = (SoapPrimitive)sum_envelope.getResponse(); 
      Log.d("sum_result", sum_results.toString()); 
      // int in = Integer.parseInt(sum_results.getProperty(0).toString()); 
      tv3.setText(""+sum_results.toString()); 
     } 
     catch (Exception e) 
     { 
      tv3.setText(e.getClass().toString()); 
      Log.d("sum_error", e.getClass().toString()); 
     } 

    } 

} 
1

我想試試這個:

  1. 服務器是一個特定的IP /端口上監聽,等待TCP連接(使用套接字)
  2. 的Android使用的TCP數據包連接到服務器,服務器現在知道Android的IP
  3. 的Android留在接收週期(使用超時TCP套接字)
  4. 服務器將數據發送到Android的IP
  5. Android從服務器接收數據

但很明顯,首先Android需要讓服務器知道它的存在。而且您還需要代碼您自己的服務器

我正在通過一個服務器做一個像這樣的中繼服務,充當我的Android應用程序和能量測量電子設備之間的橋樑。

相關問題