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 Comments

  1. www.zune.net/setup June 15, 2011 at 11:15 am

    interesting blog . It would be great if you can offer more contingents about it. Thanks you.

     
  2. Simple PHP June 22, 2011 at 12:26 am

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

     
  3. ERNESTO December 27, 2011 at 2:46 pm

    how can i post a string from android to a server using php ?!

     

Leave a Reply

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

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.