[英]Android VMD MySQL insert and showing dialog
I am trying to do a simple insert from an Android application. 我试图从Android应用程序进行简单的插入。 I can run my php
script from the browser by concatenating ?entry="Sample value from browser"
, but when I run the application from Android, I get no insert. 我可以通过连接?entry="Sample value from browser"
运行我的php
脚本,但是当我从Android运行应用程序时,我没有插入。
Here is where I am calling the insert class that uses JSON and implements AsyncTask: 这是我调用使用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();
}
}
And here is my PHP script: 这是我的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();
?>
Again, this works perfectly if I call it from the browser, but it throws an exception before implementing AsyncTask
. 同样,如果我从浏览器调用它,这可以很好地工作,但它在实现AsyncTask
之前抛出异常。
I do get the AVD to display the add and remove, but when I do that there is no request in my apache2 access_log
or error_log
. 我确实让AVD显示添加和删除,但是当我这样做时,我的apache2 access_log
或error_log
没有请求。 Any suggestions? 有什么建议么?
I think you have stored php script on local server. 我认为你已经在本地服务器上存储了php脚本。 So use this 10.0.2.2 while initializing HttpPost instead of your machine's ip address. 因此在初始化HttpPost而不是机器的ip地址时使用此10.0.2.2 。 Its localhost equivalent in android Virtual device. 它的本地主机等效于android虚拟设备。
That was not the issue for this particular problem. 这不是这个特殊问题的问题。 The issue was a magic quotes setting in the php.ini 这个问题是php.ini中的一个神奇的引号设置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.