[英]Data not saving from android app to localhost database
我已經在Eclipse Juno上創建了一個android移動應用程序,並且試圖將信息從我的移動應用程序輸入到我的php管理員數據庫中。 數據庫通過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();
?>
您完全在打印響應。 您只是在敬酒您創建的味精。 更新:您正在ui線程上運行與網絡相關的操作。 您將獲得NetworkOnMainThreadException后蜂窩。
使用線程或Asynctask 。
檢查該問題的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.