数据未从 android 应用程序保存到 localhost 数据库
Posted
技术标签:
【中文标题】数据未从 android 应用程序保存到 localhost 数据库【英文标题】:Data not saving from android app to localhost database 【发布时间】:2015-06-07 18:24:16 【问题描述】:我在 Eclipse Juno 上创建了一个 android 移动应用程序,我正在尝试将信息从我的移动应用程序输入到我的数据库中,该数据库位于 php my Admin 上。该数据库通过位于 bitnami 的 localhost wamp 服务器连接。
当我在应用程序上输入数据时,它表示已成功输入,但是当我进入数据库时,没有存储任何数据。我想知道是否有人可以指出我做错了什么。
我的应用中的代码是:
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 com.example.independentretailers.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class customersignup extends Activity
EditText etFirstName, etSurname, etPhoneNumber, etEmail, etPassword;
Button bSave;
@SuppressLint("NewApi")
@Override
protected void onCreate (Bundle savedInstanceState)
super.onCreate(savedInstanceState);
//setup strict mode policy
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.customersignup);
//for firstname
etFirstName = (EditText) findViewById(R.id.editText1);
//for surname
etSurname = (EditText) findViewById(R.id.editText2);
//for phone number
etPhoneNumber = (EditText) findViewById(R.id.editText3);
//for email
etEmail = (EditText) findViewById(R.id.editText4);
//for password
etPassword = (EditText) findViewById(R.id.editText5);
//setting up ID for the button
bSave = (Button) findViewById(R.id.button1);
//setting up onclick listener
bSave.setOnClickListener(new View.OnClickListener()
InputStream is = null;
@Override
public void onClick(View arg0)
//storing values inside edit texts inside strings
String FirstName = ""+etFirstName.getText().toString();
String Surname = ""+etSurname.getText().toString();
String PhoneNumber = ""+etPhoneNumber.getText().toString();
String Email = ""+etEmail.getText().toString();
String Password = ""+etPassword.getText().toString();
//setting name pair values
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
//adding string values inside the name value pairs
nameValuePairs.add(new BasicNameValuePair("First Name", FirstName));
nameValuePairs.add(new BasicNameValuePair("Surname", Surname));
nameValuePairs.add(new BasicNameValuePair("Phone Number", PhoneNumber));
nameValuePairs.add(new BasicNameValuePair("Email", Email));
nameValuePairs.add(new BasicNameValuePair("Password", Password));
//setting up the connection inside the try catch
try
//setting up the default client
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/tutorial.php");
//passing the name value pair inside the httppost
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
//setting up the entity
HttpEntity entity = response.getEntity();
//define input stream reader
is = entity.getContent();
//displaying message for data entered successfully
String msg = "Data entered successfully";
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
catch (ClientProtocolException e)
Log.e("ClientProtocol", "Log_tag");
e.printStackTrace();
catch (IOException e)
Log.e("Log_tag", "IOException");
e.printStackTrace();
);
我的 PHP 文本文件是:
$con=mysql_connect('localhost', 'root', 'pancakes');
mysql_select_db("independentretailers",$con);
$FirstName=$_POST['First Name'];
$Surname=$_POST['Surname'];
$PhoneNumber=$_POST['Phone Number'];
$Email=$_POST['Email'];
$Password=$_POST['Password'];
mysql_query("insert into customer(First Name, Surname, Phone Number, Email, Password) values('$FirstName','$Surname','$PhoneNumber','$Email','$Password')");
//mysql_close();
?>
【问题讨论】:
【参考方案1】:您正在打印响应。你只是在烤你创建的味精。 更新: 您正在 ui 线程上运行与网络相关的操作。您将收到 NetworkOnMainThreadException 后蜂窝。
使用线程或Asynctask。
检查这个question的答案。
【讨论】:
我在网上找到了本教程,用于从 android 应用程序将数据添加到数据库中,他们以与我相同的方式完成了操作,并且他们的数据已保存,我错过了什么吗?教程是:youtube.com/watch?v=MdyZKewSwFg 你的本地主机有密码吗? 是的,是这个问题吗?以上是关于数据未从 android 应用程序保存到 localhost 数据库的主要内容,如果未能解决你的问题,请参考以下文章
数据未从 Android 应用程序写入 Firebase 实时数据库