Php 用于发布应用程序数据的httpPost url
我想把我的数据直接从手机发送到MySQL数据库。我用php连接到MySQL。但在android代码中,我无法获取httppost中传递的url。 我的密码是Php 用于发布应用程序数据的httpPost url,php,android,mysql,Php,Android,Mysql,我想把我的数据直接从手机发送到MySQL数据库。我用php连接到MySQL。但在android代码中,我无法获取httppost中传递的url。 我的密码是 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("127.0.0.1/connect.php"); httppost.setEn
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("127.0.0.1/connect.php");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
在此httppost下设置哪个路径
请推荐!
我的php代码也是-:
<?php
$icon = mysql_connect("localhost","username","password");
if(!$icon)
{
die('Could not connect : ' . mysql_error());
}
mysql_select_db("password", $icon)or die("database selection error");
echo json_encode($data);
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$id=$_POST['id'];
$phone=$_POST['phone'];
$password=$_POST['password'];
mysql_query("INSERT INTO signup_db (firstname, lastname, id, phone, password)
VALUES ('$firstname', '$lastname', '$id', '$phone', '$password')",$icon);
mysql_close($icon);
?>
您可以在这里看到。您想要的正是使用此代码:
public class RegisterActivity extends Activity implements OnClickListener {
private EditText name, email, mobile, password;
private Button btn;
private ProgressBar pb;
String name1, email1, mobile1, password1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
mobile = (EditText) findViewById(R.id.number);
password = (EditText) findViewById(R.id.password);
btn = (Button) findViewById(R.id.signup);
pb = (ProgressBar) findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
name1 = name.getText().toString();
email1 = email.getText().toString();
mobile1 = mobile.getText().toString();
password1 = password.getText().toString();
int success = 1;// for avoiding pressing invalid parameters to website
if (name1.length() < 1) {
Toast.makeText(this, "PLEASE ENTER NAME", Toast.LENGTH_SHORT)
.show();
success = 0;
}
if (email1.length() < 1) {
Toast.makeText(this, "PLEASE PROVIDE EMAIL", Toast.LENGTH_SHORT)
.show();
success = 0;
}
if (mobile1.length() < 10) {
Toast.makeText(this, "MOBILE NUMBERS MUST BE 10 DIGITS LONG",
Toast.LENGTH_SHORT).show();
success = 0;
}
if (password1.length() < 6) {
// out of range
Toast.makeText(this, "PLEASE PROVIDE ATLEAST 6 DIGITS PASSWORD",
Toast.LENGTH_SHORT).show();
success = 0;
}
if (success == 1) {
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(name.getText().toString());
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double> {
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(name1, email1, password1, mobile1);
return null;
}
protected void onPostExecute(Double result) {
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),
"Account Activated Login To MyGenie", Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
}
protected void onProgressUpdate(Integer... progress) {
pb.setProgress(progress[0]);
}
public void postData(String name, String email, String password,
String mobile) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.mygenie.me/json/json.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs
.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
公共类RegisterActivity扩展活动实现OnClickListener{
私人编辑文本名称、电子邮件、手机、密码;
专用按钮btn;
私人ProgressBar pb;
字符串名称1、email1、mobile1、password1;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
name=(EditText)findViewById(R.id.name);
email=(EditText)findviewbyd(R.id.email);
mobile=(EditText)findViewById(R.id.number);
密码=(EditText)findViewById(R.id.password);
btn=(按钮)findViewById(R.id.signup);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(此);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共void onClick(视图v){
//TODO自动生成的方法存根
name1=name.getText().toString();
email1=email.getText().toString();
mobile1=mobile.getText().toString();
password1=password.getText().toString();
int success=1;//用于避免向网站按无效参数
if(name1.length()<1){
Toast.makeText(此“请输入名称”,Toast.LENGTH\u SHORT)
.show();
成功=0;
}
如果(email1.length()<1){
Toast.makeText(此“请提供电子邮件”,Toast.LENGTH\u SHORT)
.show();
成功=0;
}
if(mobile1.length()<10){
Toast.makeText(这是“手机号码必须有10位数长”,
吐司。长度(短)。show();
成功=0;
}
if(password1.length()<6){
//超出范围
Toast.makeText(此“请提供至少6位密码”,
吐司。长度(短)。show();
成功=0;
}
如果(成功==1){
pb.setVisibility(View.VISIBLE);
新建MyAsyncTask().execute(name.getText().toString());
}
}
私有类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的双doInBackground(字符串…参数){
//TODO自动生成的方法存根
postData(姓名1、电子邮件1、密码1、手机1);
返回null;
}
受保护的void onPostExecute(双重结果){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),
“帐户激活登录MyGenie”,Toast.LENGTH\u LONG)
.show();
意向意向=新意向(RegisterActivity.this,
物流活动(类);
星触觉(意向);
}
受保护的void onProgressUpdate(整数…进度){
pb.setProgress(progress[0]);
}
public void postData(字符串名称、字符串电子邮件、字符串密码、,
字符串(移动){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://www.mygenie.me/json/json.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“name”,name));
添加(新的BasicNameValuePair(“email”,email));
nameValuePairs
.add(新的BasicNameValuePair(“密码”,password));
添加(新的BasicNameValuePair(“mobile”,mobile));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
}
}要发送的字段名是什么?它只是一个表单-:(fname、lname、emailid、phoneNo)您需要以名称-值对的形式发送它。Httppost以关联数组的形式发送数据。我已经这样做了,并且方式正确。当我使用上述代码执行Httppost发送数据时(在try/catch中),流总是在catch块中移动。我认为我在httppost中获取的url是不正确的。先生,我做了一些相同的操作,但无法理解在httppost下获取的url路径没有一个url路径被获取数据作为关联数组发送到url,这是一种隐藏。这段代码对我来说很有用,我需要查看您的php代码以了解更多信息有关您目前需要执行的操作的信息,请检查new BasicNameValuePair(“name”,name)此处的“name”我是否应该在数据库中匹配您的表字段使用我的代码并在BasicNameValuePairs中添加新的id字段,并将您的url更改为我想这会起作用,但请确保您的php代码可以从浏览器中使用。我的php文件位于EasyHP->www->new folder->connect.php中。那么url应该是什么?live server或localhost中的php文件?让我们来看看