java - Android VMD MySQL insert and showing dialog


I am trying to do a simple insert from an Android application. I can run myphp script from the browser by concatenating?entry="Sample value from browser", but when I run the application from Android, I get no insert.

Here is where I am calling the insert class that uses JSON and implements AsyncTask:


import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class RemoteInsert extends AsyncTask<Void, String,String >{

    protected String TAG;
    protected Context context;
    protected String input;
    protected ProgressDialog progressDialog;

    public RemoteInsert(String i,Context c){
        this.input = i;
        this.context = c;

    protected void onPreExecute() {
        //ProgressDialog progressDialog; // = new ProgressDialog(context);

        //,"Please Wait..","Sending data to database", false);,"Please Wait..","Sending data to database", false);

    protected String doInBackground(Void... params) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            //HttpPost httppost = new HttpPost("http://localhost/index.php");
            //HttpPost httppost = new HttpPost("");
            HttpPost httppost = new HttpPost("");

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("entry", "Input from Android"));

            httppost.setEntity(new UrlEncodedFormEntity(postParameters));                   
            HttpResponse response = httpclient.execute(httppost);
            Log.i("postData", response.getStatusLine().toString());
        } catch(Exception e) {
            Log.e(TAG, "Error:  "+e.toString());

        return "";

    protected void onPostExecute(String result) {
        Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show(); 

And here is my PHP script:

    // mysql_connect("host","username","password");

    $entry_value = $_REQUEST["entry"];
    $query = "INSERT INTO patientdata (entry) values (".$entry_value.");";
    if( !mysql_query($query) ) { 
        /*insert failed*/ 


Again, this works perfectly if I call it from the browser, but it throws an exception before implementingAsyncTask.

I do get the AVD to display the add and remove, but when I do that there is no request in my apache2access_log orerror_log. Any suggestions?




I think you have stored php script on local server. So use this while initializing HttpPost instead of your machine's ip address. Its localhost equivalent in android Virtual device.




That was not the issue for this particular problem. The issue was a magic quotes setting in the php.ini

People are also looking for solutions to the problem: sql - PHP for repetitive large data and array processing


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.