【问题标题】:how to send data from website server to android app如何将数据从网站服务器发送到 Android 应用程序
【发布时间】:2013-06-25 08:49:04
【问题描述】:

我在一个项目中需要使用 .NET 网站为 android 应用程序配置主题。 我拥有的选项是实现从 android 应用程序到服务器的轮询服务,该服务经常轮询以查看是否需要任何更改。 任何一个更好的方式或方法可以将数据从网站发送到android应用程序而不是应用程序频繁轮询网站服务器

【问题讨论】:

  • 轮询有什么问题?我确实在我的应用程序中尝试避免(使用 web .NET 服务和 ksoap)
  • 您需要哪种类型的投票 投票您的职位?
  • @Seraphim 轮询并不是最好的选择,例如当发生变化时从服务器向客户端发送请求。
  • @UsmanKurd 其实很多东西..可能是应用程序中的一些标题文本,或应用程序的颜色等

标签: android asp.net message polling


【解决方案1】:

一个更好但更复杂的方法是使用Google Cloud Messaging(又名推送通知)。

这样,您的服务器可以通知应用程序有新数据要检索,然后您的应用程序才需要查询您的服务器。

这是一种对电池更友好的方法,而且效果很好。出于同样的原因,我以前使用过它。

也回答一些 cmets,轮询是个坏主意,因为

  • 它无缘无故地过度使用您的服务器和用户的设备
  • 它会耗尽用户的电池
  • 在服务器想要与应用程序通信的时间和您的应用程序进行下一次轮询的时间之间总会有一些延迟。

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

【讨论】:

    【解决方案2】:

    我会试试这个:

    1. 服务器正在侦听特定 IP/端口并等待 TCP 连接(使用套接字)
    2. Android 使用 TCP 数据包连接到服务器,服务器现在知道 Android IP
    3. Android 停留在接收周期中(使用超时的 TCP 套接字)
    4. 服务器向安卓IP发送数据
    5. Android 从服务器接收数据

    但很明显,首先 Android 需要让服务器知道它的存在。您还需要编写自己的服务器代码

    我正在通过服务器为中继服务做类似的事情,该服务器充当我的 Android 应用程序和能量测量电子设备之间的桥梁。

    【讨论】:

      【解决方案3】:

      您好,网上有很多这方面的教程。但无论如何,我发布代码以演示如何从 android 调用 Web 服务...此代码仅调用 SOAP Web 服务。要调用 JSON、REST 等其他 Web 服务,请在网上自行搜索。

      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());
              }
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        • 1970-01-01
        • 2013-08-21
        • 1970-01-01
        • 2023-04-06
        • 2013-04-25
        • 2018-05-11
        相关资源
        最近更新 更多