2013-06-25 131 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应用程序和能量测量电子设备之间的桥梁。

相关问题