How to communicate with php on Android

I am creating a location application where the user can parameterize some functions from the server, so I want the server to start communicating with the user's phone. But firstly, I want to open a message with android, with php.

Is there any way to connect with Android phone from php server?

I am already using a message from android from HTTP to a server with a JSONObject return, but I cannot find anything to call php for android.

I think this is just like an application that can call your phone.

+6
source share
5 answers

Go to Google Cloud Messaging for Android .

Google Cloud Messaging for Android (GCM) is a service that lets you send data from your server to your Android Android device. This can be a light message telling your application that new data should be received from the server (for example, video uploaded by a friend), or it can be a message containing up to 4 kilobytes of payload data (therefore applications such as instant messaging can consume message directly).

+1
source

GET Method

You need the Android client to connect to your server and send your JSON messages. If the client needs to get some data from the server and disconnect, you can simply use the normal HTTP GET type.

WebSocket Method

If, however, you decide that you need a long TCP connection going through JSON in a bi-directional direction, then you should consider something like WebSockets. I wrote a demo of Android WebSocket . The Android client by default connects to the websocket.org echo server, but it can be easily changed.

I also found an implementation of PHP WebSockets .

Push method

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

+1
source

Typically, creating a server-side connection on the client side is difficult because:

  • The client can use a private IP address.
  • An incoming connection may be rejected if the device is connected behind a firewall.
  • You need to install the application if it can be launched in the background and monitor the server for new messages.

Using the Web: It depends on the browser, how it supports the JavaScript API, especially the new HTML5 features like Server Sent Events

So that servers can transfer data to web pages via HTTP or using dedicated protocols with the server, this specification introduces the EventSource Interface.

0
source

Please use the link below to store data in mysql using php, and you need to create a web service where you will get two responses from php server

1) Json

2) xml

if you are showing an example, please visit the link below Creating basic web services in php

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

0
source

You can use the 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 connect and to call your php file to get 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 can use these methods to get JSon data or send it to an Android application.

 Json_decode($string); 

and

 Json_encode($string); 

I hope this helps you :)

0
source

All Articles