Android : Call PHP webservice

Leave a Comment
Web services are taking over the world. I credit Twitter's epic rise to the availability of a simple but rich API. Why not use the same model for your own sites? Here's how to create a basic web service that provides an XML or JSON response using some PHP and MySQL.

I would recommend looking into REST services. The basic structure is to have your android app preform HTTP requests(preferably in a separate thread) to the server and have the server respond with xml or json.
Heres a threaded http post class i use often.
import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import android.os.Handler; import android.os.Message; public class HttpPostThread extends Thread { public static final int FAILURE = 0; public static final int SUCCESS = 1; public static final String VKEY = "FINDURB#V0"; private final Handler handler; private String url; ArrayList<NameValuePair> pairs; public HttpPostThread(String Url, ArrayList<NameValuePair> pairs, final Handler handler) { this.url =Url; this.handler = handler; this.pairs = pairs; if(pairs==null){ this.pairs = new ArrayList<NameValuePair>(); } } @Override public void run() { try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 3000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); if(pairs!=null) post.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); String answer = EntityUtils.toString(entity); Message message = new Message(); message.obj = answer; message.what = HttpPostThread.SUCCESS; handler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); handler.sendEmptyMessage(HttpPostThread.FAILURE); } } }
Whenever you need to communicate with the server you do something like this.

Handler handler = new Handler() { @Override public void handleMessage(Message msg) { removeDialog(0); switch (msg.what) { case HttpPostThread.SUCCESS: String answer = (String)msg.obj; if (answer != null) { try { JSONObject jsonObj = new JSONObject(answer); String message = jsonObj.getString("msg"); } catch (JSONException e) { e.printStackTrace(); } } break; case HttpPostThread.FAILURE: // do some error handeling break; default: break; } } } ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("key", "value")); HttpPostThread thread = new HttpPostThread("http://serviceURL",pairs, handler); thread.start();
Below is a simple example of a php service that gets the key/value pair from the example above.
Example of a simple PHP service
<?php $value = $_POST['key']; $msg "The value".$value. "was received by the service!"; echo json_encode($msg); ?>

0 comments:

Post a Comment