如何将所有TextView和EditText字段插入Android Studio数据库MySQL中的同一列中



Allirt,让我更好地解释它。我想制作一个具有某些问题和答案的应用程序,例如TextView和EditText,在后端,我有两个列为"问题和答案"。我想在问题列中插入所有TextView(问题((问题(和所有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);
}

有很多方法可以做到。

您想在一个请求中发送所有问题并回答。为此,您可以使用 QUSONREQUEST创建 JSONREQUEST QUNERLEY

并且如果要使用 httpclient 发送,则可以发送您的这样的要求如下:

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 param中获取数据,然后解析JSON。

就是这样。

希望它对您有帮助。

使用firebase代替mysql。在firebase中,所有的iDittext和textView以列格式存储。

最新更新