How to access a remote php file from ANDROID?

By | January 19, 2011

For working on the localhost use 10.0.2.2 and for remote use the IP or domain name of the server.
The following code does this.

package com.pack;

import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class active extends Activity
{
    HttpPost httpPost;
    ResponseHandler response;
    String result;
    HttpClient htClient;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Toast.makeText(active.this,"Accessing.....", Toast.LENGTH_SHORT).show();

        htClient = new DefaultHttpClient();
        httpPost = new HttpPost("http://10.0.2.2/DB_TEST/android_test.php");
 response = new BasicResponseHandler();
 try
 {
 result = htClient.execute(httpPost,response);
 Toast.makeText(active.this,result, Toast.LENGTH_SHORT).show();
 }
 catch (ClientProtocolException e) {

 Toast.makeText(active.this,e.getMessage(), Toast.LENGTH_SHORT).show();
 }
 catch (IOException e) {
 Toast.makeText(active.this,"IO Error " + e.getMessage(), Toast.LENGTH_LONG).show();
 }
    }
}

3 thoughts on “How to access a remote php file from ANDROID?

  1. Simple PHP

    You’ve got great insights about PHP systems, keep up the good work!

    Reply

Leave a Reply to Simple PHP Cancel reply

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