如何将我的ArrayList值保存在MySQL数据库? [英] How can I store my ArrayList values to MySQL databases?

查看:1475
本文介绍了如何将我的ArrayList值保存在MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题ArrayList的值存储到MySQL数据库和PHP。这是一些code我已创建。

I have some problem to store arraylist value to mysql database with php. This is some code I have been created.

首先,我有一些类来存储变量其名为Constant.java:

First, I have some class to store variable its called Constant.java :

public class Constant {

    public static final String FIRST_COLUMN = "First";
    public static final String SECOND_COLUMN = "Second";
    public static final String THIRD_COLUMN = "Third";
    public static final String FOURTH_COLUMN = "Fourth";

}

二,我一直认为进口类MainActivity.java是这样的:

Second, I have been import that class to MainActivity.java like this :

import static com.testing.informationsystem.Constant.FIRST_COLUMN;
import static com.testing.informationsystem.Constant.SECOND_COLUMN;
import static com.testing.informationsystem.Constant.THIRD_COLUMN;
import static com.testing.informationsystem.Constant.FOURTH_COLUMN;

三,我存储我的价值通过这种方式来ArrayList的:

Third, I store my value to arraylist by this way :

        btnInsert = (Button) findViewById(R.id.btnInsert);
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                orderValue = txtOrderValue.getText().toString();
                price = txtPrice.getText().toString();

                valueSpinner = spnProductFocus.getSelectedItem().toString();

                HashMap temp = new HashMap();

                if(orderValue.equalsIgnoreCase("")){
                    orderValue = "0";
                }

                if(price.equalsIgnoreCase("")){
                    price = "1000";
                }

                double count = Double.parseDouble(orderValue) + Double.parseDouble(price);
                total = String.valueOf(count);

                if(count.equalsIgnoreCase("")){
                    count = "0";
                }

                temp.put(FIRST_COLUMN, valueSpinner);
                temp.put(SECOND_COLUMN, orderValue);
                temp.put(THIRD_COLUMN, price);
                temp.put(FOURTH_COLUMN, total);

                list.add(temp);
           }
        });

好吧,现在我有ArrayList包含的价值,我从按钮保存。现在我将通过它发送出去HTTPOST到PHP,并将其存储到MySQL。这是我的code将其发布到PHP。

Okay, now I have arraylist contains value I saved from button. And now I will send it through out HTTPOST to PHP and store it to mysql. This is my code to post it to PHP.

void saveOrder(){

        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://dcmodular.com/saktisetia/mobile/order.php");
        nameValuePairs = new ArrayList<NameValuePair>(2);

       --nameValuePairs.add(new BasicNameValuePair(arraylist));--
        ----this is my problem about how to post it to PHP---

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response=httpclient.execute(httppost);
                Log.d("Value : ", response.toString());
                HttpEntity entity=response.getEntity();
                String feedback = EntityUtils.toString(entity).trim();
                Log.d("Feedback : ", feedback);

    }

最后,这是我自己的PHP文件,将其存储到MySQL数据库:

Finally, this is my own php file to store it to mysql databases :

<?php

include('connection.php');

--receive arraylist from httppost--

$query = "insert into order(value1) values(value1)";

$execute = mysql_query($query) or die(mysql_error());

if($execute){
    echo "saved";
}else{
    echo "failed";
}

?>

这是我的code和我的问题如何张贴的ArrayList到PHP。之前谢谢您的帮扶。

That is my code and my problem how to post that arraylist to PHP. Thank you before for your helping.

推荐答案

使用HttpURLConnection的是HTTP客户端的默认不再支持。
试试这个

Use HttpUrlConnection as Http Default client is no longer supported. Try this

 public JSONObject function(String value_to_post, String  value_to_post,....){
        try {


            HashMap<String, String> params = new HashMap<>();

            params.put("your_database_field", value_to_post);



            Log.d("request", "starting");

            JSONObject json = jsonParser.makeHttpRequest(
                    your_URL, "POST", params);

            if (json != null) {
                Log.d("JSON result", json.toString());

                return json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

从你的活动中调用此函数

call this function from your activity

这篇关于如何将我的ArrayList值保存在MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆