Android – Connect to PHP ?>

Android – Connect to PHP

Search keyword: android connect to php

Here, I’m going to show the simplest way to connect to the PHP in Android – receiving JSON(JavaScript Object Notation) upon successful. You could make a simple JSON response in a PHP script like the following and deploy it to the server / localhost.

$arr = (object) array();
$arr->str = "String";
$arr->number = 1;
$arr->rightOrWrong = true;
$arr->nothing = null;
$arr->decimal = 10.209;
$arr->arr = array(1,2,3,4);
$arr->obj = (object) array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4);
$arr->title = "This is a JSON object";

echo json_encode($arr);

I’m creating a new Android project in Eclipse and extending the AsyncTask class in order to connect to PHP, and using HtppGet to make a connection to the server(can be Windows / Linux base).

package com.rndxpress.androidphpmysql;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.util.Log;

public class Conn extends AsyncTask<Void, Void, Void> {
	
	private static String convertStreamToString(InputStream is) {
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
	@Override
	protected Void doInBackground(Void... params) {
	String url = "http://[domain-name]/[json-interface]";
		
        HttpClient httpclient = new DefaultHttpClient();
 
        HttpGet httpget = new HttpGet(url); 
 
        HttpResponse response;
        try {
        	
            response = httpclient.execute(httpget);
            // Examine the response status
            Log.i("Conn::response",response.getStatusLine().toString());
 
            HttpEntity entity = response.getEntity();
            
            if (entity != null) {
            	 
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                Log.i("Conn::result",result);
 
                // A Simple JSONObject Creation
                JSONObject json=new JSONObject(result);
                Log.i("Conn::JSONObject",json.toString());
                
                instream.close();
            }
        } catch (ClientProtocolException e) {
        	Log.i("Conn::ClientProtocolException",e.getMessage());
        } catch (IOException e) {
        	Log.i("Conn::IOException",e.getMessage());
        } catch (JSONException e) {
        	Log.i("Conn::JSONException",e.getMessage());
        } catch (Error e) {
        	Log.i("Conn::Error",e.getMessage());
        } 
		return null;
	}
}

Following are the screenshot from my Galaxy Nexus & result logged in Eclipse.

Once you able to received result / response, next step you can further develop apps for more complex operations with PHP, MySQL or any further processing on the server side.

Simple UI - a button with click event to connect to the server.
Simple UI – a button with click event to connect to the server.
Logged result - JSON.toString()
Logged result – JSON.toString();

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen − eight =