我在Eclipse Juno上创建了一个Android移动应用程序,我试图从我的移动应用程序输入信息到我的数据库,这是我的管理员。数据库通过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();
?>
答案 0 :(得分:0)
你正在打印回复。你只是在烘烤你创建的msg。 更新: 您正在ui线程上运行与网络相关的操作。您将获得 NetworkOnMainThreadException 发布蜂窝。
使用主题或 Asynctask 。
检查此question的答案。