来自Android App的数据 - > MYSQL

时间:2011-09-16 04:04:57

标签: android mysql

我正在开发一个Android应用程序,用于收集数据然后将其发送到网络目录。

因此,假设想要在手机上收集数据数组,然后在单击按钮后将其作为文件或流发送到在线目录。它甚至不需要得到回应 - 尽管将来确认会很方便。

如何将数据发送到mysql

我提交我的代码如下

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    InputStream inputStream;

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        Button btn = (Button)findViewById(R.id.button1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 nameValuePairs.add(new BasicNameValuePair("Lat","19.80"));
                 nameValuePairs.add(new BasicNameValuePair("Lon","13.22"));

                 try{

                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://localhost/guestfeedback/connection.php");
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);



                        HttpEntity entity = response.getEntity();
                        InputStream is = entity.getContent();
                        Log.i("postData", response.getStatusLine().toString());


                 }catch (Exception e) {
                    // TODO: handle exception
                }
        }

1 个答案:

答案 0 :(得分:0)

我个人推荐IntentServiceHttpPost。关于它们的文档很容易理解。