使用POST方法将数据发送到PHP?

时间:2012-10-15 20:12:17

标签: java php android http post

我正在尝试将4个变量发布到Web服务器上的PHP文件中。这是我在eclipse中构建的Android应用程序。

当我打开应用程序运行此功能的那一刻,应用程序崩溃了。在添加此功能之前,应用程序运行正常。该应用程序在模拟器中运行良好,但只在我的手机上崩溃。我尝试在三星S3和S2上运行它。

Android应用中的功能。使用log.d我能够发现应用程序运行到try语句而不是崩溃。

public void postData( double pLong, double pLat, String timeStamp) throws ClientProtocolException, IOException, URISyntaxException {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://website.com/this.php");
    Log.d("response", "WORKING");
    try {

        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("IMEI", deviceid));
        nameValuePairs.add(new BasicNameValuePair("LONGITUDE",Double.toString(pLat)));
        nameValuePairs.add(new BasicNameValuePair("LATITUDE", Double.toString(pLong)));
        nameValuePairs.add(new BasicNameValuePair("CREATED_AT", timeStamp));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        Log.d("response!!!!", "  " + response);

    } catch (ClientProtocolException e) {
        //Requirement Auto-generated catch block
        Log.d("response", "nope" + e);
    } catch (IOException e) {
        Log.d("response", "nope" + e);
        //Requirement Auto-generated catch block
    }
} 

PHP文件:

<?php
$imei = $_POST['IMEI'];
$long = $_POST['LONGITUDE'];
$lat = $_POST['LATITUDE'];
$time = $_POST['CREATED_AT'];

$con = mysql_connect("host", "username", "password");  //this is the real username and password

if (!$con)
  {
      die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("database", $con);

mysql_query("INSERT INTO `table`(`imei`, `longitude`, `latitude`, `createdAt`)
VALUES ('".$imei."', '".$long."', '".$lat."', '".$time."')") or die (mysql_error());

$id = mysql_insert_id();
echo "done";
?>

1 个答案:

答案 0 :(得分:1)

创建一个新类,实例化它和.execute()。 为什么会出现此错误?因为你需要使它异步。

private class MyPost extends AsyncTask<Void,Void,Void>{

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://cdobiz.com/submit/");

        try {
            // Add your data

            EditText txtName = (EditText)findViewById(R.id.txtBusinessName);
            EditText txtDesc = (EditText)findViewById(R.id.txtDescription);

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("frmName",txtName.getText().toString() ));
            nameValuePairs.add(new BasicNameValuePair("frmDesc", txtDesc.getText().toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            Log.v("Post Status","Code: "+response.getStatusLine().getStatusCode());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }

}