Android VMD MySQL插入和显示对话框

Jas*_*lor 5 php java android android-asynctask android-dialog

我试图从Android应用程序进行简单的插入.我可以php通过连接从浏览器运行我的脚本?entry="Sample value from browser",但是当我从Android运行应用程序时,我没有插入.

这是我调用使用JSON并实现AsyncTask的insert类的地方:

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(); 
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的PHP脚本:

<?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();
?>
Run Code Online (Sandbox Code Playgroud)

同样,如果我从浏览器调用它,这可以很好地工作,但它在实现之前会抛出异常AsyncTask.

我确实让AVD显示添加和删除,但是当我这样做时,我的apache2 access_log或者没有请求error_log.有什么建议?

Rut*_*Raj 0

我认为您已将 php 脚本存储在本地服务器上。因此在初始化 HttpPost 时使用这个10.0.2.2而不是你机器的 IP 地址。它的 localhost 相当于 android 虚拟设备。