1

我是java和PHP的新手,有人可以帮助我的数据库只显示0 ...

爪哇代码:

公共类 postData 扩展 Activity {

//Progress Dialog
private ProgressDialog pDialog;

//JSONParser jsonParser = new JSONParser();

//url to update coordinates
private static String url_update_coordinates = "http://www.myurl.com";

//JSON Node names
private static final String TAG_SUCCESS = "Success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView (R.layout.post_coords);

    final Context ctx = this;

        //Create button
        Button btnUploadCoordinates = (Button) findViewById(R.id.button5);

        //Button click event
        btnUploadCoordinates.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // updating coordinates on background thread
                new UploadCoordinates(ctx).execute();

            }
        });

    }

    //Background Async Task to upload coordinates

    class UploadCoordinates extends AsyncTask <String, String, String> {

        // Before starting background thread Show Progress Dialog
        private Context ctx;
        public UploadCoordinates(Context ctx) {
            this.ctx = ctx;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(postData.this);
            pDialog.setMessage("Uploading Coordinates...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        //Creating Coordinates

        @Override
        protected String doInBackground(String... params) {


            JSONArray json = new JSONArray();



            MySQLite dbhelper = new MySQLite(ctx);
            Cursor data = dbhelper.getlocations();

            while(data.moveToNext()) {

                int _id = data.getInt(0);
                double latitude = data.getDouble(1);
                double longitude = data.getDouble(2);
                double altitude = data.getDouble(3);
                double speed = data.getDouble(4);
                double timestamp = data.getDouble(5);

                JSONObject jo = new JSONObject();
                try{
                jo.put("_id", _id);
                jo.put("latitude", latitude);
                jo.put("longitude", longitude);
                jo.put("altitude", altitude);
                jo.put("speed", speed);
                jo.put("timestamp", timestamp);
                } catch(JSONException e) {

                }

                json.put(jo);
            }
            String json_data = json.toString();


        // Adding the data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("coords", json_data));


        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.myurl.com");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        //Check log for response
        Log.d("Create response", json.toString());
            return null;
        }

// 检查成功标签

尝试 { int 成功 = json_data.getInt(TAG_SUCCESS);

在这里给我问题 对于 String 类型的方法 getInt(String) 未定义

                    if (success == 1) {
                        // successfully created product
                        Intent i = new Intent(getApplicationContext(), GPSLoggerService.class);
                        startActivity(i);

                        // closing this screen
                        finish();
                    } else {
                        // failed to create product
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }


        //After completion background task Dismiss progress dialog
        protected void onPostExecute(String file_url) {
            //dismiss the dialog once done
            pDialog.dismiss();
        }


    }

}

php脚本:

<?php
ini_set('error_reporting', E_ALL); ini_set('display_errors','1');
//include dbconnect class
    require_once  (__DIR__ . '/db_connect.php');

    //connecting to db
    $db = new DB_CONNECT();     

    //decode array
    $arr = (isset($_POST['coords']));
    $decarr = json_decode($arr, true);

    $count = count($decarr);

    $values = array(); //hold array values so we do one single insert

    $update_values = array(); //holds values for the ON DUPLICATE KEY UPDATE

    for ($x=0; $x <$count; $x++)
    {
        $newrec = $decarr[$x];
        $_id = $newrec['_id']; $_id = mysql_real_escape_string($_id);
        $latitude = $newrec['latitude']; $_id = mysql_real_escape_string($latitude);
        $longitude = $newrec['longitude']; $_id = mysql_real_escape_string($longitude);
        $timestamp = $newrec['timestamp']; $_id = mysql_real_escape_string($timestamp);
        $altitude = $newrec['altitude']; $_id = mysql_real_escape_string($altitude);
        $speed = $newrec['speed']; $_id = mysql_real_escape_string($speed);

        //create insert array
        $values[] = "('".$_id."','".$latitude."','".$longitude."','".$timestamp."','".$altitude."','".$speed."')";

        //For the duplicate updates
        $update_values[]=
        "latitude=VALUES(latitude), longitude=VALUES(longitude), timestamp=VALUES(timestamp),                               altitude=VALUES(altitude), speed=VALUES(speed)";
    }

    //insert records
    $sql = "INSERT INTO logs(_id, latitude, longitude, timestamp, altitude, speed)
            VALUES ".implode(',', $values)." ON DUPLICATE KEY UPDATE ".implode(',',$update_values);

            $result = mysql_query($sql);

?>

已经尝试了几个小时,但无法弄清楚问题出在哪里,也许这对你们中的许多人来说是一个愚蠢的问题。

提前感谢您的所有帮助。

问候五

更新 - 不知道我是否应该这样做,但它会更容易,因为所有代码都已经在这里,我的错误是在块引用之间......无法让我的头脑看到问题所在......任何帮助表示赞赏.

4

1 回答 1

1

问题是您使用isset函数并将其返回值分配给 $arr 变量,然后将其用作数组。

您应该使用此函数来确定变量是否已设置且不为 NULL:它返回 true 或 false。

在您的 PHP 代码中,而不是第 10 行和第 11 行,试试这个:

$decarr = isset($_POST['coords']) ? json_decode($_POST['coords'], true) : array();
于 2013-07-13T16:13:41.427 回答