0

I have an Android application that sends data via HTTP POST method to a PHP script and am trying to parse the data to store into a MySQL. The Android app was not written by me but I have access to the source code which I have included below; it sends the data packed as a JSON array. My PHP script right now gets the raw data and writes it to a text file for the time being:

$filename = __DIR__.DIRECTORY_SEPARATOR."jsontest.txt";
$postdata = file_get_contents("php://input"); 
file_put_contents($filename, "$postdata \n", FILE_APPEND);

The text file writes out the data like this:

{"records":[{"name":"accelerator_pedal_position","value":15.400001,"timestamp":1367598908.278000},{"name":"engine_speed","value":1716.0,"timestamp":1367598908.285000},{"name":"vehicle_speed","value":32.040001,"timestamp":1367598908.290000},{"name":"brake_pedal_status","value":false,"timestamp":1367598908.293000},{"name":"fuel_consumed_since_restart","value":0.147325,"timestamp":1367598908.301000},{"name":"transmission_gear_position","value":"third","timestamp":1367598908.304000},{"name":"steering_wheel_angle","value":-2.3733,"timestamp":1367598908.307000},{"name":"fuel_consumed_since_restart","value":0.14745,"timestamp":1367598908.314000},{"name":"transmission_gear_position","value":"third","timestamp":1367598908.317000},{"name":"door_status","value":"driver","event":false,"timestamp":1367598908.320000},{"name":"door_status","value":"passenger","event":false,"timestamp":1367598908.326000},{"name":"door_status","value":"rear_left","event":false,"timestamp":1367598908.329000},{"name":"door_status","value":"rear_right","event":false,"timestamp":1367598908.331000},{"name":"odometer","value":0.0,"timestamp":1367598908.338000},{"name":"high_beam_status","value":false,"timestamp":1367598908.341000},{"name":"steering_wheel_angle","value":-2.3733,"timestamp":1367598908.343000},{"name":"engine_speed","value":1716.0,"timestamp":1367598908.351000},{"name":"powertrain_torque","value":74.0,"timestamp":1367598908.358000},{"name":"accelerator_pedal_position","value":12.1,"timestamp":1367598908.364000},{"name":"latitude","value":42.293911,"timestamp":1367598908.367000},{"name":"longitude","value":-83.238762,"timestamp":1367598908.376000},{"name":"engine_speed","value":1718.0,"timestamp":1367598908.380000},{"name":"vehicle_speed","value":32.200001,"timestamp":1367598908.382000},{"name":"brake_pedal_status","value":false,"timestamp":1367598908.391000},{"name":"transmission_gear_position","value":"third","timestamp":1367598908.393000}]} 

This is just one of many "records" that are received. I have a MySQL database set up with Name, Value, Event, and Timestamp columns and want each of these to be insert into there as they are received. I have tried reading the data using $_POST and using json_decode but to no avail. Below is the Android code for sending the data:

public class UploaderSink extends ContextualVehicleDataSink {
private final static String TAG = "UploaderSink";
private final static int UPLOAD_BATCH_SIZE = 25;
private final static int MAXIMUM_QUEUED_RECORDS = 5000;
private final static int HTTP_TIMEOUT = 5000;

private URI mUri;
private BlockingQueue<String> mRecordQueue =
        new LinkedBlockingQueue<String>(MAXIMUM_QUEUED_RECORDS);
private Lock mQueueLock = new ReentrantLock();
private Condition mRecordsQueued = mQueueLock.newCondition();
private UploaderThread mUploader = new UploaderThread();

/**
 * Initialize and start a new UploaderSink immediately.
 *
 * @param uri the URI to send HTTP POST requests to with the JSON data.
 */
public UploaderSink(Context context, URI uri) {
    super(context);
    mUri = uri;
}

public UploaderSink(Context context, String path) throws DataSinkException {
    this(context, uriFromString(path));
}

@Override
public void stop() {
    super.stop();
    mUploader.done();
}

public boolean receive(RawMeasurement measurement) {
    String data = measurement.serialize(true);
    mRecordQueue.offer(data);
    if(mRecordQueue.size() >= UPLOAD_BATCH_SIZE) {
        mQueueLock.lock();
        mRecordsQueued.signal();
        mQueueLock.unlock();
    }
    return true;
}

/**
 * Returns true if the path is not null and if it is a valid URI.
 *
 * @param path a URI to validate
 * @return true if path is a valid URI.
 *
 */
public static boolean validatePath(String path) {
    if(path == null) {
        Log.w(TAG, "Uploading path not set (it's " + path + ")");
        return false;
    }

    try {
        uriFromString(path);
        return true;
    } catch(DataSinkException e) {
        return false;
    }
}

@Override
public String toString() {
    return Objects.toStringHelper(this)
        .add("uri", mUri)
        .add("queuedRecords", mRecordQueue.size())
        .toString();
}

private static URI uriFromString(String path) throws DataSinkException {
    try {
        return new URI(path);
    } catch(java.net.URISyntaxException e) {
        throw new UploaderException(
            "Uploading path in wrong format -- expected: ip:port");
    }
}

private static class UploaderException extends DataSinkException {
    private static final long serialVersionUID = 7436279598279767619L;

    public UploaderException() { }

    public UploaderException(String message) {
        super(message);
    }
}

private class UploaderThread extends Thread {
    private boolean mRunning = true;

    public UploaderThread() {
        start();
    }

    public void run() {
        while(mRunning) {
            try {
                ArrayList<String> records = getRecords();
                String data = constructRequestData(records);
                HttpPost request = constructRequest(data);
                makeRequest(request);
            } catch(UploaderException e) {
                Log.w(TAG, "Problem uploading the record", e);
            } catch(InterruptedException e) {
                Log.w(TAG, "Uploader was interrupted", e);
                break;
            }
        }
    }

    public void done() {
        mRunning = false;
    }

    private String constructRequestData(ArrayList<String> records)
            throws UploaderException {
        StringWriter buffer = new StringWriter(512);
        JsonFactory jsonFactory = new JsonFactory();
        try {
            JsonGenerator gen = jsonFactory.createJsonGenerator(buffer);

            gen.writeStartObject();
            gen.writeArrayFieldStart("records");
            Iterator<String> recordIterator = records.iterator();
            while(recordIterator.hasNext()) {
                gen.writeRaw(recordIterator.next());
                if(recordIterator.hasNext()) {
                    gen.writeRaw(",");
                }
            }
            gen.writeEndArray();
            gen.writeEndObject();

            gen.close();
        } catch(IOException e) {
            Log.w(TAG, "Unable to encode all data to JSON -- " +
                    "message may be incomplete", e);
            throw new UploaderException();
        }
        return buffer.toString();
    }

    private HttpPost constructRequest(String data)
            throws UploaderException {
        HttpPost request = new HttpPost(mUri);
        try {
            ByteArrayEntity entity = new ByteArrayEntity(
                    data.getBytes("UTF8"));
            entity.setContentEncoding(
                    new BasicHeader("Content-Type", "application/json"));
            request.setEntity(entity);
        } catch(UnsupportedEncodingException e) {
            Log.w(TAG, "Couldn't encode records for uploading", e);
            throw new UploaderException();
        }
        return request;
    }

    private void makeRequest(HttpPost request) throws InterruptedException {
        HttpParams parameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(parameters, HTTP_TIMEOUT);
        HttpConnectionParams.setSoTimeout(parameters, HTTP_TIMEOUT);
        final HttpClient client = new DefaultHttpClient(parameters);
        try {
            HttpResponse response = client.execute(request);
            final int statusCode = response.getStatusLine().getStatusCode();
            if(statusCode != HttpStatus.SC_CREATED) {
                Log.w(TAG, "Got unxpected status code: " + statusCode);
            }
        } catch(IOException e) {
            Log.w(TAG, "Problem uploading the record", e);
            try {
                Thread.sleep(5000);
            } catch(InterruptedException e2) {
                Log.w(TAG, "Uploader interrupted after an error", e2);
                throw e2;
            }
        }
    }

    private ArrayList<String> getRecords() throws InterruptedException {
        mQueueLock.lock();
        if(mRecordQueue.isEmpty()) {
            // the queue is already thread safe, but we use this lock to get
            // a condition variable we can use to signal when a batch has
            // been queued.
            mRecordsQueued.await();
        }

        ArrayList<String> records = new ArrayList<String>();
        mRecordQueue.drainTo(records, UPLOAD_BATCH_SIZE);

        mQueueLock.unlock();
        return records;
    }
}

}

4

1 回答 1

3

第一个代码段中的$postdata变量包含一个 JSON 字符串。首先你想用json_decode.

$data = json_decode($postdata, true);

as 第二个参数使用JSON 结构中的对象而不是 PHP 对象true创建关联数组。{}如果您愿意,可以使用 PHP 对象,对以下代码进行一些修改(例如->name,而不是['name']

现在,记录位于顶级键“记录”中,它是您要插入数据库的对象的索引数组。您可以像这样遍历记录:

if (array_key_exists('records', $data) && is_array($data['records'])) {
    foreach ($data['records'] as $record) {
        $name = $record['name'];
        // etc for other properties, then do what you want with them, e.g.
        echo "Got name of '$name' with value of '$value'";
        // or insert into database, or whatever
    }
}

(更新:我if在上面添加了语句以允许输入不包含“记录”键或它不是数组)

于 2013-05-06T07:49:23.193 回答