-2

4つの変数をWebサーバー上のPHPファイルに投稿しようとしています。これは、私がEclipseで構築しているAndroidアプリ用です。

この関数を実行した瞬間にアプリを開くと、アプリがクラッシュします。この関数を追加する前は、アプリは正常に機能していました。アプリはエミュレーターで完璧に動作しますが、私の電話でのみクラッシュします。SamsungS3とS2の両方で実行してみました。

Androidアプリで機能します。log.diを使用すると、アプリがクラッシュするよりも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";
?>
4

1 に答える 1

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

}
于 2012-10-17T04:57:26.343 に答える