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(); } } } |
interesting blog . It would be great if you can offer more contingents about it. Thanks you.
You’ve got great insights about PHP systems, keep up the good work!
how can i post a string from android to a server using php ?!