【问题标题】:How to insert all textview and edittext fields into same columns in database mysql in Android Studio如何在 Android Studio 中将所有 textview 和 edittext 字段插入数据库 mysql 的相同列中
【发布时间】:2023-04-08 07:32:01
【问题描述】:

好吧,让我更好地解释一下。 我想制作一个应用程序,它具有特定的问题和答案集作为 textview 和 edittext,并且在后端我有两列名为问题和答案。我想在问题列中插入所有 textview(question),在答案列中插入所有 edittext(answers)。我可以一次插入一行,但不能在单击按钮时一次插入所有行。

public void SendDataToServer(final String name, final String email, final String website){
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {

            String QuickName = name ;
            String QuickEmail = email ;
            String QuickWebsite = website;

            //String[] str = {QuickName,QuickEmail,QuickWebsite};

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("name", QuickName));
            //nameValuePairs.add(new BasicNameValuePair("name", email));

            nameValuePairs.add(new BasicNameValuePair("email", QuickEmail));
            nameValuePairs.add(new BasicNameValuePair("website", QuickWebsite));

            try {
                HttpClient httpClient = new DefaultHttpClient();

                HttpPost httpPost = new HttpPost(DataParseUrl);

                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity entity = response.getEntity();


            } catch (ClientProtocolException e) {

            } catch (IOException e) {

            }
            return "Data Submit Successfully";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            Toast.makeText(MainActivity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();

        }
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(name, email, website);
}

【问题讨论】:

    标签: php android mysql


    【解决方案1】:

    有很多方法可以做到这一点。

    您想在一个请求中发送所有问题和答案。为此,您可以创建一个JsonRequest using Volley

    如果您想使用 HttpClient 发送,那么您可以发送您的 像这样请求 JsonString:

    JSONObject rootObject = new JSONObject();
    JSONArray dataArr = new JSONArray();
    for (int i = 0; i < questions.length; i++) // loop will execute total no. of questions
        {
            JSONObject itemObj = new JSONObject();
            itemObj.put("question", "What is your name?");
            itemObj.put("answer", "XYZ");
    
            dataArr.put(itemObj);
        }
        rootObject.put("data", dataArr);
    
        // Finally send your all data here
        nameValuePairs.add(new BasicNameValuePair("request", rootObject.toString()));
    

    在服务器端,您将从 request 参数中获取数据,然后解析 json。

    就是这样。

    希望对你有帮助。

    【讨论】:

    • 谢谢你的快速回复兄弟..但这是我不明白 itemObj.put("question", "What is your name?");为什么我们有问答的价值为“你叫什么名字?”和“xyz”?在引号中..这是什么意思?
    • 这是您将插入要发送的 JsonObject 中的值。您不能在请求中发送 TextView,您可以从 TextView 获取文本并插入到 JsonObject 中。
    【解决方案2】:

    使用 firebase 而不是 mysql。在firebase中,所有edittext和textview都以列格式存储。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 2011-12-13
      • 2016-03-03
      • 2014-12-07
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多