如何将所有textview和edittext字段插入到Android Studio中的数据库mysql的相同列中

如何将所有textview和edittext字段插入到Android Studio中的数据库mysql的相同列中

问题描述:

Allright let me explain it better. I want to make an app which has certain set of questions and answers as textview and edittext and in backend i have two columns named as question and answers. I want to insert all textview(question) in question columns and all edittext(answers) in answers columns. Im able to insert one row at a time but not all at one time on button click.

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);
}

好吧,让我更好地解释一下。 我想创建一个具有一定问题和答案的应用程序作为textview 和edittext在后端我有两列名为问题和答案。 我想在问题列中插入所有textview(问题),在answer列中插入所有edittext(答案)。 我可以一次插入一行,但不能一次插入按钮。 p>

  public void SendDataToServer(final String name,final String email,final String website){  
类SendPostReqAsyncTask扩展AsyncTask&lt; String,Void,String&gt;  {
 @Override 
 protected String doInBackground(String ... params){
 
 String QuickName = name; 
 String QuickEmail = email; 
 String QuickWebsite = website; 
 
 // String []  str = {QuickName,QuickEmail,QuickWebsite}; 
 
 List&lt; NameValuePair&gt;  nameValuePairs = new ArrayList&lt; NameValuePair&gt;(); 
 
 nameValuePairs.add(new BasicNameValuePair(“name”,QuickName)); 
 //nameValuePairs.add(new BasicNameValuePair(“name”,email)); 
  
 nameValuePairs.add(new BasicNameValuePair(“email”,QuickEmail)); 
 nameValuePairs.add(new BasicNameValuePair(“website”,QuickWebsite)); 
 
尝试{
 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){
 
} 
返回“数据提交成功”; 
} 
 
  @Override 
 protected void onPostExecute(String result){
s  uper.onPostExecute(result); 
 
 Toast.makeText(MainActivity.this,“Data Submit Successfully”,Toast.LENGTH_LONG).show(); 
 
} 
} 
 SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask(  ); 
 sendPostReqAsyncTask.execute(名称,电子邮件,网站); 
} 
  code>  pre> 
  div>

There are many ways to do it.

You want to send all question and answer in one request. And for this you can create a JsonRequest using Volley

And If you want to send using HttpClient then you can send your request as JsonString like this:

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()));

and on the server side you will get your data from request param and then parse the json.

That's it.

Hope it will help you.

use firebase instead of mysql. In firebase all edittext and textview stored in column format.