2014-10-01 33 views
0

我已經創建了由我希望各國財長將保存到我的遠程server.Thanks提前如何將FUNF探針保存到遠程服務器?

公共類只使用基本的探頭就像wifi和簡單location.At數據保存到SD卡的那一刻起funf應用MainActivity擴展活動實現的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); 
     } 
    }); 
} 

}

回答

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!"; 
    } 
?> 
0

您需要在服務器數據庫和一些後臺功能將數據添加到您的遠程數據庫。後端功能應該是服務器,你可以從你的Android應用程序通過的HttpRequest等稱之爲..閱讀關於REST API的

+0

如果我創建數據庫我怎麼知道我的領域,並再次在那裏把後端功能 – walters 2014-10-01 14:11:08

+0

你什麼意思,你怎麼會知道你的領域?例如,您應該編寫PHP函數來調用它,以打開數據庫,並在數據中插入一行。然後從android發送HTTPRequest(或POST),並使用應用程序中的數據調用該函數。 PHP函數獲取您發送的數據,並在一切正常時返回響應。 – 2014-10-01 16:29:51

+0

Hi @walters Hi,你有答案嗎?如果沒有請平安我,我可以幫助你。 – vipin 2014-10-22 12:18:40