我想通過http響應中的post方法將數據從我們的android應用程序傳遞到網站。如何將數據從android發送(發佈)到網站或服務器
我現在想要的是 - 我給一個添加按鈕,如果我點擊添加它會打開一個編輯活動,它具有編輯文本和保存按鈕,鍵入一些數據或文本,然後單擊保存按鈕它必須去該網站,我不再需要數據庫也從我們的應用程序發送或發佈數據在網站上。
Mainactivity.java
public class MainActivity extends ListActivity implements FetchDataListener
{
private static final int ACTIVITY_CREATE=0;
private ProgressDialog dialog;
private ProjectsDbAdapter mDbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_list_item);
mDbHelper = new ProjectsDbAdapter(this);
mDbHelper.open();
//fillData();
//registerForContextMenu(getListView());
initView();
}
private void initView()
{
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://floating-wildwood-1154.herokuapp.com/posts.json";
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
mDbHelper.open();
Cursor projectsCursor = mDbHelper.fetchAllProjects();
//startManagingCursor(projectsCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{ProjectsDbAdapter.KEY_TITLE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1};
/* Now create a simple cursor adapter and set it to display
SimpleCursorAdapter projects =
new SimpleCursorAdapter(this, R.layout.activity_row, projectsCursor, from, to);
setListAdapter(projects);
*/
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.activity_row,
projectsCursor,
from,
to,
0);
setListAdapter(dataAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
createProject();
return super.onMenuItemSelected(featureId, item);
}
private void createProject() {
Intent i = new Intent(this, ProjectEditActivity.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
initView();
}
@Override
public void onFetchComplete(List<Application> data)
{
// dismiss the progress dialog
if (dialog != null)
dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}
@Override
public void onFetchFailure(String msg)
{
// dismiss the progress dialog
if (dialog != null)
dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
projecteditactivity.java
public class ProjectEditActivity extends Activity {
private EditText mTitleText;
private Button mConfirmButton;
private Long mRowId;
private ProjectsDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new ProjectsDbAdapter(this);
setContentView(R.layout.activity_project_edit);
mTitleText = (EditText) findViewById(R.id.title);
mConfirmButton = (Button) findViewById(R.id.confirm);
mRowId = savedInstanceState != null ? savedInstanceState.getLong(ProjectsDbAdapter.KEY_ROWID)
: null;
registerButtonListenersAndSetDefaultText();
}
private void setRowIdFromIntent() {
if (mRowId == null || mRowId.longValue() == 0) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(ProjectsDbAdapter.KEY_ROWID)
: null;
}
}
@Override
protected void onPause() {
super.onPause();
mDbHelper.close();
}
@Override
protected void onResume() {
super.onResume();
mDbHelper.open();
setRowIdFromIntent();
populateFields();
}
private void registerButtonListenersAndSetDefaultText() {
mConfirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveState();
setResult(RESULT_OK);
Toast.makeText(ProjectEditActivity.this, getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show();
finish();
}
});
}
private void populateFields() {
if (mRowId != null) {
Cursor Project = mDbHelper.fetchProject(mRowId);
startManagingCursor(Project);
mTitleText.setText(Project.getString(
Project.getColumnIndexOrThrow(ProjectsDbAdapter.KEY_TITLE)));
Project.close();
}
else {
// This is a new task - add defaults from preferences if set.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String defaultTitleKey = getString(R.string.pref_task_title_key);
String defaultTitle = prefs.getString(defaultTitleKey, null);
if(defaultTitle != null)
mTitleText.setText(defaultTitle);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(ProjectsDbAdapter.KEY_ROWID, mRowId);
}
private void saveState() {
String title = mTitleText.getText().toString();
//if (mRowId == null)
if (mRowId == null || mRowId.longValue() == 0)
{
long id = mDbHelper.createProject(title);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateProject(mRowId, title);
}
}
}
這裏我使用分貝,但我不希望我的應用程序使用的數據庫,什麼都我打字在編輯文本和點擊保存按鈕後,它必須傳遞或發送數據到服務器,我怎麼能在我的應用程序中做到這一點。
嘗試搜索如何在谷歌發送服務器上的數據。這會給你很多鏈接。 –
你可以得到我的要求@ChintanRathod,如何在我的代碼中做一個兄弟,而不使用db –