httpPost网址发布应用数据

时间:2013-11-29 09:59:08

标签: php android mysql

我想将我的数据直接从我的手机发送到MySQL数据库。我用php连接MySQL.But在android代码中我无法得到哪个url在httppost内传递。 我的代码是。

 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);
?>

2 个答案:

答案 0 :(得分:1)

使用此代码:

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
        }
    }

}

}

答案 1 :(得分:0)

你可以在这里看到。你想要的是什么Send Data to server from android