简体   繁体   中英

How to communicate from php to Android

I am making a location application, where user can parameter some function from the server, so I want the server to begin a communication with the phone of the user. But firstly, I want to open a communication with an android, from the php.

Is there a way to communicate with an android phone from a php server?

I already use the communication from android with HTTP to server with return of JSONObject , but I cant find anything for a php call to android.

I think its exactly like the application which can make your phone ring.

The GET method

You will need to have the Android client connect to your server and pass your JSON messages. If the client needs to get some data from the server and disconnect, then you can just use a normal HTTP type GET.

The WebSocket method

If however, you decide you need a long running TCP connection passing JSON bidirectionally then you should consider something like WebSockets. I have written an Android WebSocket demo . The Android client by default connects to the websocket.org echo server, but that can be easily changed.

I also found a PHP WebSockets implementation .

The Push Method

Now if your plan is to push messages from the server to the client without the client initiating the connection you will need something like GCM (Google Cloud Messaging). Here is an article covering GCM and PHP .

Check out Google Cloud Messaging for Android .

Google Cloud Messaging for Android (GCM) is a service that allows you to send data from your server to your users' Android-powered device. This could be a lightweight message telling your app there is new data to be fetched from the server (for instance, a movie uploaded by a friend), or it could be a message containing up to 4kb of payload data (so apps like instant messaging can consume the message directly).

Generally, creating connection from server side to client side is complex, because:

  1. The client might use private IP address.
  2. Inbound connection might be rejected if the device connected behind firewall.
  3. You need to install an application if that can be run in the background and watches the server for new messages.

Using Web: It depends on the browser how it support JavaScript API especially new HTML5 features such as Server Sent Events

To enable servers to push data to Web pages over HTTP or using dedicated server-push protocols, this specification introduces the EventSource interface.

Please use below link for store data in mysql using php and u need to create webservice in that you will get two response from php server

1) Json

2) xml

if you show example please visit below link Creating a basic web services in php

also visit this link for better description http://phpmaster.com/lets-talk-1/

You Can Use HTTpReq class :

public class HttpReq {

public   String send (String url){
    //send a http request and get the result    
    InputStream is = null;
    String result = "";

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

      try
        {
           BufferedReader reader = new BufferedReader(new InputStreamReader(is),8);
           StringBuilder sb = new StringBuilder();
           String line = null;

           while ((line = reader.readLine()) != null) 
            {
               sb.append(line + "\n");
            }

             is.close();
             result=sb.toString();

         }catch(Exception e){
        Log.e("log_tag", "Error converting result " + e.toString());
    }


    return result;
}

}

Then you use this class to make connection and to call your php file to get the data as JSonObject .

 ht = new HttpReq();
             // send a http request with GET 
             x=ht.send("http://10.0.2.2/myFolder/myFile.php");
             JSONArray jArray;
            JSONObject json_data;

         String h[]=x.split("<");

           try {

                jArray = new JSONArray(h[0]);
                json_data = jArray.getJSONObject(0);
                url=json_data.getString("url");

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

In Your Php file you may use this methods to get the JSon data or to send it to the android App

Json_decode($string); 

And

Json_encode($string);

I hope that will help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM