John Pierre
John Pierre

Reputation: 21

To get data from a php file to an android application

I am a newbie in using android applications. I would like to get data from a php file to android and I do not know how to do it.

I will use a simple example of a php code which I want to get into android:

$array = array(0 => 'zero', 1 => 'one', 2 => 'two'); 
print $array;

This script is here: http://johnyho.net/index.php

Can you please give me an advice how to get this field into the android. Thank you very much.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

AndroidManifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.android.websevice.client.samples"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidClientService"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Upvotes: 2

Views: 3150

Answers (2)

Muhammad Usman
Muhammad Usman

Reputation: 12503

Use HttpClient

HttpClient hc = new DefaultHttpClient();
HttpClient hc = new DefaultHttpClient();
HttpGet post = new HttpGet(" http://johnyho.net/index.php");
HttpResponse rp = hc.execute(post);
if (rp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
    // Server is unavailable
}

str = EntityUtils.toString(rp.getEntity()); //here is your response

Please import necessary namespaces like

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

Upvotes: 1

Y.A.P.
Y.A.P.

Reputation: 538

Consider JSON. HERE is a PHP JSON documentation, and HERE is a connection example from Android to any JSON url wchich might be your URL.

Upvotes: 2

Related Questions