java - Android VMD MySQL insert and showing dialog

623

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:

package us.jtaylorok.android.sqlite.first;

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.app.ProgressDialog;
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);

        //progressDialog=ProgressDialog.show(,"Please Wait..","Sending data to database", false);
        progressDialog=ProgressDialog.show(context,"Please Wait..","Sending data to database", false);
    }

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

            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) {
        progressDialog.dismiss(); 
        Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show(); 
    }
}

And here is my PHP script:

<?php
    // mysql_connect("host","username","password");
    mysql_connect("localhost","user1","mypassword");
    mysql_select_db("test");

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

    mysql_close();
?>

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?

924

Answer

Solution:

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

480

Answer

Solution:

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

Source

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.