如何将FUNF探针保存到远程服务器?

时间:2014-10-01 13:15:13

标签: android

我创建的funf应用程序只使用基本探测,如wifi和简单的位置。目前数据保存到SD卡,我希望它们保存到我的远程服务器。谢谢提前

public class MainActivity extends Activity实现DataListener {

public static final String PIPELINE_NAME = "default";
private FunfManager funfManager;
private BasicPipeline pipeline;
private WifiProbe wifiProbe;
private SimpleLocationProbe locationProbe;
private CheckBox enabledCheckbox;
private Button archiveButton, scanNowButton;
private TextView dataCountView;
private Handler handler;
private ServiceConnection funfManagerConn = new ServiceConnection() {    
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        funfManager = ((FunfManager.LocalBinder)service).getManager();

        Gson gson = funfManager.getGson();
        wifiProbe = gson.fromJson(new JsonObject(), WifiProbe.class);
        locationProbe = gson.fromJson(new JsonObject(), SimpleLocationProbe.class);
        pipeline = (BasicPipeline) funfManager.getRegisteredPipeline(PIPELINE_NAME);
        wifiProbe.registerPassiveListener(MainActivity.this);
        locationProbe.registerPassiveListener(MainActivity.this);

        // This checkbox enables or disables the pipeline
        enabledCheckbox.setChecked(pipeline.isEnabled());
        enabledCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (funfManager != null) {
                    if (isChecked) {
                        funfManager.enablePipeline(PIPELINE_NAME);
                        pipeline = (BasicPipeline) funfManager.getRegisteredPipeline(PIPELINE_NAME);
                    } else {
                        funfManager.disablePipeline(PIPELINE_NAME);
                    }
                }
            }
        });

        // Set UI ready to use, by enabling buttons
        enabledCheckbox.setEnabled(true);
        archiveButton.setEnabled(true);

        scanNowButton.setEnabled(true);

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        funfManager = null;

    }



};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 // Forces the pipeline to scan now
    scanNowButton = (Button) findViewById(R.id.scanNowButton);
    scanNowButton.setEnabled(false);
    scanNowButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (pipeline.isEnabled()) {
                // Manually register the pipeline
                wifiProbe.registerListener(pipeline);
                locationProbe.registerListener(pipeline);
            } else {
                Toast.makeText(getBaseContext(), "Pipeline is not enabled.", Toast.LENGTH_SHORT).show();
            }
        }
    });

    // Displays the count of rows in the data
    dataCountView = (TextView) findViewById(R.id.dataCountText);

    // Used to make interface changes on main thread
    handler = new Handler();

    enabledCheckbox = (CheckBox) findViewById(R.id.enabledCheckbox);
    enabledCheckbox.setEnabled(false);

    // Runs an archive if pipeline is enabled
    archiveButton = (Button) findViewById(R.id.archiveButton);
    archiveButton.setEnabled(false);
    archiveButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (pipeline.isEnabled()) {
                pipeline.onRun(BasicPipeline.ACTION_ARCHIVE, null);

                // Wait 1 second for archive to finish, then refresh the UI
                // (Note: this is kind of a hack since archiving is seamless and there are no messages when it occurs)
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(), "Archived!", Toast.LENGTH_SHORT).show();
                        updateScanCount();
                    }
                }, 1000L);
            } else {
                Toast.makeText(getBaseContext(), "Pipeline is not enabled.", Toast.LENGTH_SHORT).show();
            }
        }
    });

    // Bind to the service, to create the connection with FunfManager
    bindService(new Intent(this, FunfManager.class), funfManagerConn, BIND_AUTO_CREATE);
}

@Override
public void onDataCompleted(IJsonObject probeConfig, JsonElement checkpoint) {
    updateScanCount();

    // Re-register to keep listening after probe completes.
    wifiProbe.registerPassiveListener(this);
    locationProbe.registerPassiveListener(this);
}


@Override
public void onDataReceived(IJsonObject arg0, IJsonObject arg1) {
    // TODO Auto-generated method stub

}
private static final String TOTAL_COUNT_SQL = "SELECT count(*) FROM " + NameValueDatabaseHelper.DATA_TABLE.name;
/**
* Queries the database of the pipeline to determine how many rows of data we have recorded so far.
*/
private void updateScanCount() {
    // Query the pipeline db for the count of rows in the data table
    SQLiteDatabase db = pipeline.getDb();
    Cursor mcursor = db.rawQuery(TOTAL_COUNT_SQL, null);
    mcursor.moveToFirst();
    final int count = mcursor.getInt(0);
    // Update interface on main thread
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            dataCountView.setText("Data Count: " + count);
        }
    });
}

}

2 个答案:

答案 0 :(得分:0)

您需要服务器上的数据库和一些后端功能才能将数据添加到远程数据库。后端函数应该在服务器上,你可以通过HttpRequest等从你的Android应用程序调用它。阅读REST API

答案 1 :(得分:0)

要将数据发送到远程服务器,首先需要配置strings.xml

文件如下。

"archive": {
                "@schedule": {"interval": 60}
           },
"upload":  {
                   "url": \"http://example.com/test/android_data_receiver.php\",
                   "@schedule": {"interval": 60}
           }

它会每1分钟向服务器发送一次数据,并确保已添加

访问远程服务器到Android清单文件的权限

允许许可的代码

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.INTERNET" />  

完成上述步骤后,请在您的域中创建一个服务器文件,对于

测试目的我在下面创建了一个文件。哟可以根据需要修改文件。

<?php

   $target_path  = "uploads/";
   $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

   if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
   } 
   else {
        echo "There was an error uploading the file, please try again!";
   }
?>