I am creating a survey app in which i am using checkboxes and textviews to show to the questions which i am retrieving from server
数据库现在我想插入被调查者的回复 进入服务器数据库。被调查的人可以选择多个 回答。请帮助我java文件,因为我无法得到 逻辑。
我的.XML文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.fortune.axismouseproj.SurveyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout">
<LinearLayout android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Logo Start-->
<ImageView android:src="@drawable/header"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#232b5a"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<!-- Logo Ends -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="#c8d6f0">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Project"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:id="@+id/tvproject" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="330dp"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvsurvey" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="35dp"
android:id="@+id/chkopt1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvoptone"
android:paddingLeft="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="35dp"
android:id="@+id/chkopt2"
android:layout_marginBottom="4dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="@+id/tvopttwo"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="35dp"
android:id="@+id/chkopt3"
android:layout_marginBottom="4dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="@+id/tvoptthree"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="35dp"
android:id="@+id/chkopt4"
android:layout_marginBottom="4dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="@+id/tvoptfour"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="15dp">
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:id="@+id/btnsubmtnxt"
android:background="@drawable/axisnext"
android:layout_gravity="center_horizontal|right"
android:layout_marginTop="15dp" />
<Button
android:layout_width="match_parent"
android:layout_height="45dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:id="@+id/btnsumbit"
android:background="@drawable/axissubexit"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp" />
<Button
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/btncontinue"
android:background="@drawable/axissubcontt"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout android:src="@drawable/footer1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#232b5b"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/imageView2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#c8d6f0"
android:weightSum="1"
android:gravity="center"
android:layout_above="@+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/link"
android:onClick="linkdn"
android:background="@drawable/axislink"
/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/twit"
android:onClick="twiiter"
android:layout_marginLeft="40dp"
android:background="@drawable/axistwitter" />
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/fb"
android:layout_marginLeft="40dp"
android:onClick="facebook"
android:background="@drawable/axisfacebook" />
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/go"
android:onClick="insta"
android:layout_marginLeft="40dp"
android:background="@drawable/axisinsta"/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/go2"
android:layout_marginLeft="40dp"
android:onClick="youtube"
android:background="@drawable/axisyoutube"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="30dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:text="Axis Mouse © 2016-2017"
android:textSize="10sp"
android:textColor="#ffffff"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
来自服务器java代码的我的数据检索代码
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
public class SurveyActivity extends Activity {
Button submit,conti,nextbtn;
TextView head,survey,optn1,optn2,optn3,optn4;
String que,opt1,opt2,opt3,opt4;
CheckBox checkBox1,checkBox2,checkBox3,checkBox4;
int surveyno=127;
int questionno=1;
public final static String TAG_SUCCESS = "success";
public static final String EXTRA_MESSAGE = "Message";
String msg;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
submit = (Button) findViewById(R.id.btnsumbit);
conti = (Button) findViewById(R.id.btncontinue);
head = (TextView) findViewById(R.id.tvproject);
nextbtn = (Button) findViewById(R.id.btnsubmtnxt);
survey = (TextView) findViewById(R.id.tvsurvey);
optn1 = (TextView) findViewById(R.id.tvoptone);
optn2 = (TextView) findViewById(R.id.tvopttwo);
optn3 = (TextView) findViewById(R.id.tvoptthree);
optn4 = (TextView) findViewById(R.id.tvoptfour);
checkBox1 = (CheckBox) findViewById(R.id.chkopt1);
checkBox2 = (CheckBox) findViewById(R.id.chkopt2);
checkBox3 = (CheckBox) findViewById(R.id.chkopt3);
checkBox4 = (CheckBox) findViewById(R.id.chkopt4);
nextbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
do {
new FetchQuestion().execute();
questionno++;
} while (questionno >= 22);
}
});
Typeface type01=Typeface.createFromAsset(getAssets(),"HelveticaNeue-UltraLight.ttf");
head.setTypeface(type01);
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent fours1 = new Intent(SurveyActivity.this, BeginAction.class);
startActivity(fours1);
}
});
conti.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent fours1 = new Intent(SurveyActivity.this, EntrpNxtStart.class);
startActivity(fours1);
}
});
new FetchQuestion().execute();
}
public void linkdn(View view)
{
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.linkedin.com"));
startActivity(intent);
}
public void facebook(View view)
{
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com"));
startActivity(intent);
}
public void twiiter(View view)
{
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/"));
startActivity(intent);
}
public void insta(View view)
{
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.instagram.com/"));
startActivity(intent);
}
class FetchQuestion extends AsyncTask<String, String, String>
{
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(SurveyActivity.this);
progressDialog.setTitle("Contacting Servers");
progressDialog.setMessage("Logging in ...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected String doInBackground(String... args)
{
System.out.println("Doinbackground entered!");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("questionno",String.valueOf(questionno)));
params.add(new BasicNameValuePair("surveyno",String.valueOf(surveyno)));
JSONObject json = jsonParser.makeHttpRequest("http://localhost/ws/survey.php","POST", params);
System.out.println("json object made, php should exec now!" + json.toString());
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
System.out.println(" Details fetched Successfully!");
String msg = json.getString("message");
System.out.println(" msg " + msg);
que = json.getString("question");
System.out.println(" que " + que);
opt1 = json.getString("option1");
System.out.println(" opt1 " + opt1);
opt2 = json.getString("option2");
System.out.println(" opt2 " + opt2);
opt3 = json.getString("option3");
System.out.println(" opt3 " + opt3);
opt4 = json.getString("option4");
System.out.println(" opt4 " + opt4);
}
else
{
System.out.print("UnSuccessfull ");
msg = json.getString("message");
System.out.print(msg);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url)
{
progressDialog.dismiss();
survey.setText(que);
optn1.setText(opt1);
optn2.setText(opt2);
optn3.setText(opt3);
optn4.setText(opt4);
checkBox1.setChecked(false);
checkBox2.setChecked(false);
checkBox3.setChecked(false);
checkBox4.setChecked(false);
}
}
}
这是我想要插入数据的Insert.PHP代码 serverdatabase
<?php
$response = array();
$servername = "localhost";
$username = "root";
$password = "R00t1975#";
$dbname = "tikox";
$conn = mysqli_connect($servername, $username, $password,
$dbname);
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
echo "success";
}
$userid = isset($_POST['userid']) ? $_POST['userid'] :
'';
$surveyno = isset($_POST['surveyno']) ? $_POST
['surveyno'] : '';
$questionno=isset($_POST['questionno']) ? $_POST
['questionno'] : '';
$optionno=isset($_POST['optionno']) ? $_POST['optionno']:
'';
$query = mysqli_query($connect, "insert into
answerindividual (userid,surveyno,questionno,optionno)
values
('$userid','$surveyno','$questionno','$optionno') ");
// check if row inserted or not
if ($query) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "data successfully inserted.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
mysqli_close($connect);
?>
在我的数据库中有四个字段userinfo,surveyno,questionno, answerno。所以我想将这四个值插入数据库。请 帮助我编写用于插入服务器数据库的Java代码