如何将数据(填写在我的Android应用程序中的应用程序表格)发送到网站(我的本地主机)?

时间:2013-12-07 17:42:05

标签: android json localhost toast

我是一名新的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()

2 个答案:

答案 0 :(得分:0)

  1. 您需要获取字段的值。假设它们是EditText,请编写类似yourEditText.getText().toString()的内容,将其解析为字符串并获取您要发送的字段。
  2. 要发送它们,您可能需要阅读Android Volley Tutorial - Making HTTP GET, POST, PUT, DELETE Requests

答案 1 :(得分:0)

按照以下几个步骤

1)将数据收集到变量或数组中

2)与数据库建立连接

3)让您的数据库处于可写模式

4)fire insert查询并关闭数据库

相关问题