Java android中MySql数据库插入数组

Java android中MySql数据库插入数组,java,android,mysql,arrays,database,Java,Android,Mysql,Arrays,Database,这是一个MultiAutocompleteTextView,它从用户那里获取fav运动,运动现在存储在数组中,我想将这些值存储在MySQL数据库的数组中 这是我的密码: String[] str = {"Foot Ball", "Volley Ball", "Basket Ball", "Golf", "F1", "Marathon "}; final MultiAutoCompleteTextView mt = (MultiAutoCompleteTex

这是一个MultiAutocompleteTextView,它从用户那里获取fav运动,运动现在存储在数组中,我想将这些值存储在MySQL数据库的数组中

这是我的密码:

   String[] str = {"Foot Ball", "Volley Ball", "Basket Ball",
            "Golf", "F1", "Marathon "};

    final MultiAutoCompleteTextView mt = (MultiAutoCompleteTextView)
            findViewById(R.id.MyTags);

    mt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, str);
    mt.setThreshold(1);
    mt.setAdapter(adp);


    Button add=(Button)findViewById(R.id.add);
    assert add != null;
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = mt.getText().toString();

            if (text != null && text.length() > 0) {
                text = text.substring(0, text.length() - 1);
            }
            String sports[] = text.split(",");  // countries array will have all the countries entered in multiAutoCompleteTextView


                for (String s : countries) {
                    Toast.makeText(Profile.this, s, Toast.LENGTH_SHORT).show();

            }

        }
    });

}
String[]str={“足球”、“排球”、“篮球”,
“高尔夫”、“F1”、“马拉松”};
最终MultiAutoCompleteTextView mt=(MultiAutoCompleteTextView)
findviewbyd(R.id.MyTags);
mt.setTokenizer(新的MultiAutoCompleteTextView.CommaTokenizer());
ArrayAdapter adp=新的ArrayAdapter(这是android.R.layout.simple\u下拉列表\u item\u 1line,str);
mt.setThreshold(1);
mt.setAdapter(adp);
按钮添加=(按钮)findViewById(R.id.add);
断言添加!=无效的
add.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串text=mt.getText().toString();
if(text!=null&&text.length()>0){
text=text.substring(0,text.length()-1);
}
String sports[]=text.split(,“”;//countries数组将在multiAutoCompleteTextView中输入所有国家/地区
用于(字符串s:国家){
Toast.makeText(Profile.this,s,Toast.LENGTH_SHORT).show();
}
}
});
}

如果android中没有MySql数据库,您可以在android上的Sqlite上实现,也可以尝试使用webservice在MySql服务器上发布数据

要从MultiAutoCompleteTextView获取多个分割文本,并将其存储在数组中,然后从android将其传递给MySql:

 public class WorkNow extends AppCompatActivity {
public static final String URL_ADD = "YOUR LINK";
public static String KEY_C="tags";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_work_now);

    Button add = (Button) findViewById(R.id.buttonAdd);


    String[] str = {"India", "China", "Japan",
            "Nepal", "Italy", "Germany "};

    final MultiAutoCompleteTextView mt = (MultiAutoCompleteTextView)
            findViewById(R.id.multiAutoCompleteTextView1);

    mt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, str);
    mt.setThreshold(1);
    mt.setAdapter(adp);


    assert add != null;
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = mt.getText().toString();

            if (text != null && text.length() > 0) {
                text = text.substring(0, text.length() - 1);
            }
            String countries[] = text.split(",");  // countries array will have all the countries entered in multiAutoCompleteTextView

            for (final String s : countries) {
                Toast.makeText(WorkNow.this, s, Toast.LENGTH_SHORT).show();

                class AddEmployee extends AsyncTask<Void, Void, String> {

                    ProgressDialog loading;

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        loading = ProgressDialog.show(WorkNow.this, "Adding...", "Wait...", false, false);
                    }

                    @Override
                    protected void onPostExecute(String s) {
                        super.onPostExecute(s);
                        loading.dismiss();
                        Toast.makeText(WorkNow.this, s, Toast.LENGTH_LONG).show();
                    }

                    @Override
                    protected String doInBackground(Void... v) {
                        HashMap<String, String> params = new HashMap<>();
                        params.put(KEY_C, s);


                        RequestHandler rh = new RequestHandler();
                        String res = rh.sendPostRequest(URL_ADD, params);
                        return res;
                    }
                }

                AddEmployee ae = new AddEmployee();
                ae.execute();
            }
        }
    });
}


    @Override
    public boolean onCreateOptionsMenu (Menu menu){
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}
公共类WorkNow扩展了AppCompatActivity{
公共静态最终字符串URL\u ADD=“您的链接”;
公共静态字符串键\u C=“tags”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u work\u now);
按钮添加=(按钮)findViewById(R.id.ButtonAd);
字符串[]str={“印度”、“中国”、“日本”,
“尼泊尔”、“意大利”、“德国”};
最终MultiAutoCompleteTextView mt=(MultiAutoCompleteTextView)
findViewById(R.id.multiAutoCompleteTextView1);
mt.setTokenizer(新的MultiAutoCompleteTextView.CommaTokenizer());
ArrayAdapter adp=新的ArrayAdapter(这是android.R.layout.simple\u下拉列表\u item\u 1line,str);
mt.setThreshold(1);
mt.setAdapter(adp);
断言添加!=null;
add.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串text=mt.getText().toString();
if(text!=null&&text.length()>0){
text=text.substring(0,text.length()-1);
}
字符串countries[]=text.split(,“”;//countries数组将在multiAutoCompleteTextView中输入所有国家/地区
for(最终字符串s:国家){
Toast.makeText(WorkNow.this,s,Toast.LENGTH_SHORT).show();
类AddEmployee扩展异步任务{
对话加载;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loading=ProgressDialog.show(WorkNow.this,“添加…”,“等待…”,false,false);
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
loading.dispose();
Toast.makeText(WorkNow.this,s,Toast.LENGTH_LONG).show();
}
@凌驾
受保护字符串背景(无效…v){
HashMap params=新的HashMap();
参数put(键C,s);
RequestHandler rh=新的RequestHandler();
String res=rh.sendPostRequest(URL_ADD,params);
返回res;
}
}
AddEmployee ae=新的AddEmployee();
ae.execute();
}
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
}
下面是PHP代码:

<?php 
$con = mysqli_connect("YOUR_HOST_NAME", "USER_ID", "PASSWOR", "YOUR_DATABASE");
if($_SERVER['REQUEST_METHOD']=='POST'){

    //Getting values
    $tags = $_POST['tags'];



    //Creating an sql query
    $sql = "INSERT INTO tags (tags) VALUES ('$tags')";

    //Importing our db connection script


    //Executing query to database
    if(mysqli_query($con,$sql)){
        echo 'Employee Added Successfully';
    }else{
        echo 'Could Not Add Employee';
    }

    //Closing the database 
    mysqli_close($con);
}

是的,仅使用外部Mysql服务器当您接收到外部Mysql服务器上的数据时,您可以编写将数据作为“post”或“get”参数的Web服务。您可以使用任何基于web的语言来实现这一点。(Asp.net、PHP、Java等)如果您想尝试在android设备中保存数据,请不要使用sqlite或Realm。若要在服务器上保存数据,则需要为其创建web服务,并调用web服务将数据发布到服务器。