how to send data from website server to android app

二次信任 提交于 2020-01-15 12:33:28

问题


I had a requirement in a project to configure a theme for android application using a .NET website. The options that i have is to implement a polling service from android app to server which polls frequently to see if any change is required. Can any one point any better way or method to send data from a website to android app rather than the app polling the website server frequently


回答1:


A better but more complex way to do it is to use Google Cloud Messaging (aka Push Notifications).

This way your server can notify the app that there are new data to be retrieved, and only then your app has to query your server.

This is a much more battery friendly approach and works pretty well. I've used this before for the same reason.

To answer some of the comments too, polling is a bad idea because

  • it overuses both your server and the user's device for no reason
  • it will drain the user's battery
  • there will always be some delay between when the server wants to communicate with the app and the time your app will do the next polling.

The push notification approach takes more effort but has great advantages too.




回答2:


I would try this:

  1. Server is listening on a specific IP/port and wait for TCP connection (using a socket)
  2. Android connect to server using TCP packets, server now know Android IP
  3. Android stay in a receive cycle (using TCP socket with timeouts)
  4. Server send data to Android IP
  5. Android receive data from server

But it's clear that first Android needs to let the server know about its presence. And you also need to code your own server.

I'm doing something like this for a relay service through a server that acts as a bridge between my Android app and an energy measurement electronics.




回答3:


Hi there are lots of tutorial for this on Internet. But anyways i am posting the code to demonstrate that how you can call the web service from android...This code is to call only SOAP web service. To call other web services like JSON,REST etc search yourself on net.

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

    }

}


来源:https://stackoverflow.com/questions/17292929/how-to-send-data-from-website-server-to-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!