【问题标题】:Send data using POST method android to PHP?使用POST方法android向PHP发送数据?
【发布时间】:2012-10-15 20:12:17
【问题描述】:

我正在尝试将 4 个变量发布到网络服务器上的 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";
?>

【问题讨论】:

  • 堆栈跟踪显示什么?
  • 您使用的是哪个安卓操作系统?
  • 又一个networkonmainthread异常问题
  • 您是否使用主线程将数据 POST 到您的 PHP 服务器?
  • 是的,我正在尝试将数据发布到服务器。跟踪运行到 "Log.d("response", "WORKING");"比它崩溃。我在模拟器上使用 android 2.2,在手机上使用 4.0.3。

标签: java php android http post


【解决方案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;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多