我是一名新的android开发人员。我正在构建一个应用程序,其中填写了一份注册表,其内容数据将存储在我的本地主机上。我已经制作了表格,但我不知道如何编码将数据发送到本地主机。帮助我从基本代码,这样当点击发送按钮时,表格中填写的数据就会存储在本地主机上。
这是我的代码。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_server);
send1 = (Button)findViewById(R.id.Send);
send1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
testInput ti=new testInput();
//Create the intent
ti.postData("Sent Data");
TextView num1View = (TextView) findViewById(R.id.T5);
num1View.setText("your data is stored");
//Create the intent
}
});
}
public void postData(String toPost) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("my local host name");
//This is the data to send
String name = toPost;
String number= toPost;
String email =toPost;
String suggestion= toPost;//any data to send
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", name));
nameValuePairs.add(new BasicNameValuePair("action", number));
nameValuePairs.add(new BasicNameValuePair("action", email));
nameValuePairs.add(new BasicNameValuePair("action", suggestion));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
//This is the response from a php application
String reverseString = response;
Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
} catch (IOException e) {
Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
}
}//end postData()
遵循以下几个步骤
1) 将数据收集到变量或阵列中
2) 与数据库建立连接
3) 使您的数据库处于可写模式
4) 启动插入查询并关闭数据库
- 您需要获取字段的值。假设它们是
EditText
,那么编写类似于yourEditText.getText().toString()
的内容,将其解析为String,并获取要发送的字段 - 要发送它们,您可能需要阅读Android Volley教程-制作HTTP GET、POST、PUT、DELETE请求