我正在制作一个需要在 MySQL 数据库中发布一些数据的应用程序。该代码未显示任何错误,但未发送任何数据。我的 php 文件和 HttpPost 似乎工作正常 - 我尝试更改 php 文件,以便它已经包含数据,然后它工作。这是我的php:
<?php
$username = "user";
$password = "password";
$hostname = "mysql.xxx.com"; 
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("myapp_xxx_com",$dbhandle) 
  or die("Could not select examples");
//retrieve the data
$street = $_POST['Street'];
$house = $_POST['House'];
$city = $_POST['City'];
$comment = $_POST['Comment'];
mysql_query ("INSERT INTO Address (Street, Number, City, Comment, TimeOrdered) VALUES('$street', '$house, '$city', '$comment', NOW())");
mysql_close($dbhandle);
?>
这是我的java代码:
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 android.annotation.TargetApi;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OrderSummary extends Activity implements OnClickListener {
    private EditText editStreetText, editNumberText, editCityText, editCommentText;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_summary);
        editStreetText = (EditText) findViewById(R.id.summary_street);
        editNumberText = (EditText) findViewById(R.id.summary_house);
        editCityText = (EditText) findViewById(R.id.summary_city);
        editCommentText = (EditText) findViewById(R.id.summary_comment);
        button = (Button)findViewById(R.id.button_post_data);
        button.setOnClickListener(this);
    @Override
    public void onClick(View v) {
        String streetValue = editStreetText.getText().toString();
        String numberValue = editNumberText.getText().toString();
        String cityValue = editCityText.getText().toString();
        String commentValue = editCommentText.getText().toString();
        new SummaryAsyncTask().execute(streetValue, numberValue, cityValue, commentValue);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
    public void postData(String street, String number, String city, String comment)
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xxxxxxx.com/postdata.php");
        try{
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("Street", street));
            nameValuePairs.add(new BasicNameValuePair("House", number));
            nameValuePairs.add(new BasicNameValuePair("City", city));
            nameValuePairs.add(new BasicNameValuePair("Comment", comment));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error:  "+e.toString());
        } 
    }
    private class SummaryAsyncTask extends AsyncTask<String, Void, Void>{
        protected Void doInBackground(String... params){
            postData(params[0], params[1], params[2], params[3]);
            return null;
        }
    }
    }
}
我的代码基于本教程http://mobiledevtuts.com/android/android-http-with-asynctask-example/。我希望有人可以帮助我。