有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用Java/安卓发送选择题问卷(包括可能的答案)

大家好,我已经搜索了很多,但找不到任何满足我想为我的项目做什么的答案。也许我不知道该找什么,但他是: 项目:持续的课堂反馈

我正在尝试编写一个程序,使讲师能够(从java应用程序)向学生的手机(安卓应用程序)发送一份选择题问卷(一个包含4个可能答案的问题),这样他们就可以触摸答案,单击“提交”,然后将其发送回老师

我只是不知道怎么做。我做了单选按钮,但我被卡住了,因为我不知道如何发送它们(如果可能的话)。 还有一件事你建议我用什么来制定最适合我的计划?我用过WI-Fi,但我遇到了一个问题,每次我都要在这里和那里使用端口

我已准备好做出您建议的任何更改,但请尝试为我找到解决方案:)

这是我到目前为止的代码:(抱歉,如果我把这个问题提错了,这是我第一次这样做)

安卓软件包

欢迎。爪哇:

package mobileapp.com;

import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.os.Handler;

public class Welcome extends Activity {
private Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.welcome);
    handler.postDelayed(loadNextWindow, 3000);  
}

private Runnable loadNextWindow = new Runnable(){
    public void run(){
        Intent intent = new Intent(Welcome.this,Answer.class);
        startActivity(intent);
    }

};

}

回答。爪哇:

package mobileapp.com;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.widget.Button;
import 安卓.widget.RadioButton;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

public class Answer extends Activity {
private TextView qtext;
private RadioButton rB1, rB2, rB3, rB4;
private Button sendButton, connectButton;
private Socket socket;
private String serverIpAddress = "";
private String answer;
// AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO
   // PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S
   // PORT 6000
private static final int REDIRECTED_SERVERPORT = 7076;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {


    answer = null;
    super.onCreate(icicle);
    setContentView(R.layout.answer);

    qtext = (TextView) findViewById(R.id.textQuestion1);
    rB1 = (RadioButton) findViewById(R.id.rButton1);
    rB2 = (RadioButton) findViewById(R.id.rButton2);
    rB3 = (RadioButton) findViewById(R.id.rButton3);
    rB4 = (RadioButton) findViewById(R.id.rButton4);
    sendButton = (Button) findViewById(R.id.button1);
    connectButton = (Button) findViewById(R.id.button2);


    connectButton.setOnClickListener(new OnClickListener(){

        public void onClick(View v2) {
            // TODO Auto-generated method stub

        try {
            BufferedReader  br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            qtext.setText(br.toString());

        }
     catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
     }

         }

    });




    sendButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (rB1.isChecked()){
                Toast t= Toast.makeText(getApplicationContext(), rB1.getText().toString()+ " sent!", Toast.LENGTH_LONG);
                t.show();
                answer = "1st Choice";

            }
            if (rB2.isChecked()){
                Toast t= Toast.makeText(getApplicationContext(), rB2.getText().toString()+ " sent!", Toast.LENGTH_LONG);
                t.show();
                answer = "2nd  Choice";

            }
            if (rB3.isChecked()){
                Toast t= Toast.makeText(getApplicationContext(), rB3.getText().toString()+ " sent!", Toast.LENGTH_LONG);
                t.show();
                answer = "3rd  Choice";

            }
            if (rB4.isChecked()){
                Toast t= Toast.makeText(getApplicationContext(), rB4.getText().toString()+ " sent!", Toast.LENGTH_LONG);
                t.show();
                answer = "4th  Choice";

            }
            try {
                InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            try {
            DataOutputStream oos = new DataOutputStream(socket.getOutputStream());
            oos.writeBytes(answer);
            oos.close();
                //Toast toast3 = Toast.makeText(getBaseContext(), "Message was sent successfully!", Toast.LENGTH_LONG);
                //toast3.show();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {

                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

}
}

欢迎。xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 安卓:id="@+id/LinearLayout01"
安卓:layout_width="fill_parent" 安卓:layout_height="fill_parent" 
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓" 安卓:orientation="vertical"
安卓:background="@drawable/evening" 
>


<TextView 安卓:text="@string/welcomeMessage" 安卓:layout_width="fill_parent" 安卓:layout_height="fill_parent" 
安卓:textStyle="bold" 安卓:textSize="25sp" 安卓:textColor="#FFFFFF"
安卓:gravity="center"
></TextView>



</LinearLayout>

回答。xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 安卓:id="@+id/LinearLayout01"
安卓:layout_width="fill_parent" 安卓:layout_height="fill_parent"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"

>
<ScrollView 安卓:layout_width="fill_parent" 安卓:layout_height="fill_parent" 安卓:scrollbarAlwaysDrawVerticalTrack="true" 
安卓:scrollbarStyle="outsideOverlay">
<LinearLayout 安卓:id="@+id/LinearLayout02"
安卓:layout_width="fill_parent" 安卓:layout_height="fill_parent"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical" 安卓:scrollbars="vertical" 安卓:scrollbarAlwaysDrawVerticalTrack="true"
>
    <TextView 安卓:id="@+id/textQuestion1" 安卓:layout_width="fill_parent" 安卓:text="@string/textQuestion" 安卓:layout_height="wrap_content"></TextView>
<RadioGroup 安卓:id="@+id/radioGroup01" 
安卓:orientation="vertical" 安卓:layout_height="wrap_content" 安卓:layout_width="fill_parent"  >
<RadioButton 安卓:id="@+id/rButton1" 安卓:text="@string/c1Label" 安卓:layout_height="wrap_content" 安卓:layout_width="fill_parent"> </RadioButton>
<RadioButton 安卓:id="@+id/rButton2" 安卓:text="@string/c2Label" 安卓:layout_height="wrap_content" 安卓:layout_width="fill_parent"> </RadioButton>
<RadioButton 安卓:id="@+id/rButton3" 安卓:text="@string/c3Label" 安卓:layout_height="wrap_content" 安卓:layout_width="fill_parent"> </RadioButton>
<RadioButton 安卓:id="@+id/rButton4" 安卓:text="@string/c4Label" 安卓:layout_height="wrap_content" 安卓:layout_width="fill_parent"> </RadioButton>
</RadioGroup>
<Button 安卓:text="Submit" 安卓:id="@+id/button1" 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content"></Button>
<Button 安卓:text="Connect" 安卓:id="@+id/button2" 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content"></Button>
</LinearLayout>

</ScrollView>
</LinearLayout>

标签。xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont">
    <item name="安卓:layout_width">fill_parent</item>
    <item name="安卓:layout_height">fill_parent</item>
    <item name="安卓:textColor">#00FF00</item>
    <item name="安卓:singleLine">false</item>
     <item name="安卓:lines">2</item>
</style>
</resources>

弦。xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Please answer quickly</string>
<string name="c1Label">First choice</string>
<string name="c2Label">Second choice</string>
<string name="c3Label">Third choice</string>
<string name="c4Label">Fourth choice</string>
<string name="welcomeMessage">Welcome!</string>

<string name="textQuestion">No question yet...</string>
</resources>

这是一个java类,用来将问题发送到安卓应用程序

服务器应用。爪哇:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.net.*;

public class ServerApp implements Runnable {
// Connect status constants
public final static int NULL = 0;
public final static int DISCONNECTED = 1;
public final static int DISCONNECTING = 2;
public final static int BEGIN_CONNECT = 3;
public final static int CONNECTED = 4;

// Other constants
public final static String statusMessages[] = {
  " Error! Could not connect!", " Disconnected",
  " Disconnecting...", " Connecting...", " Connected"
};
public final static ServerApp tcpObj = new ServerApp();
public final static String END_SESSION =
  new Character((char)0).toString(); // Indicates the end of a session

// Connection atate info
public static String hostIP = "localhost";
public static int port = 7076;
public static int connectionStatus = DISCONNECTED;
public static boolean isAnswerMode = true;
public static String statusString = statusMessages[connectionStatus];
public static StringBuffer toAppend = new StringBuffer("");
public static StringBuffer toSend = new StringBuffer("");

// Various GUI components and info
public static JFrame mainFrame = null;
public static JTextArea getText = null;
public static JTextField outText = null;
public static JCheckBox cbAnswer1 = null;
public static JCheckBox cbAnswer2 = null;
public static JCheckBox cbAnswer3 = null;
public static JCheckBox cbAnswer4 = null;
public static JPanel statusBar = null;
public static JLabel statusField = null;
public static JTextField statusColor = null;
public static JTextField ipField = null;
public static JTextField portField = null;
public static JRadioButton answerMode = null;
public static JRadioButton questionMode = null;
public static JButton connectButton = null;
public static JButton disconnectButton = null;

// TCP Components
public static ServerSocket hostServer = null;
public static Socket socket = null;
public static BufferedReader in = null;
public static PrintWriter out = null;

//members
public static String questionText = null;

/////////////////////////////////////////////////////////////////

private static JPanel initOptionsPane() {
  JPanel pane = null;
  ActionAdapter buttonListener = null;

  // Create an options pane
  JPanel optionsPane = new JPanel(new GridLayout(6, 1));

  //Window label
  pane = new JPanel(new FlowLayout(FlowLayout.LEFT));
  pane.add(new JLabel("SERVER APP"));
  //pane.add(ipField);
  optionsPane.add(pane);

  // IP address input
  pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  pane.add(new JLabel("Host IP:"));
  ipField = new JTextField(10); 
  ipField.setText(hostIP);
  ipField.setEnabled(false);
  ipField.addFocusListener(new FocusAdapter() {
        public void focusLost(FocusEvent e) {
           ipField.selectAll();
           // Should be editable only when disconnected
           if (connectionStatus != DISCONNECTED) {
              changeStatusNTS(NULL, true);
           }
           else {
              hostIP = ipField.getText();
           }
        }
     });
  pane.add(ipField);
  optionsPane.add(pane);

  // Port input
  pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  pane.add(new JLabel("Port:"));
  portField = new JTextField(10); portField.setEditable(true);
  portField.setText((new Integer(port)).toString());
  portField.addFocusListener(new FocusAdapter() {
        public void focusLost(FocusEvent e) {
           // should be editable only when disconnected
           if (connectionStatus != DISCONNECTED) {
              changeStatusNTS(NULL, true);
           }
           else {
              int temp;
              try {
                 temp = Integer.parseInt(portField.getText());
                 port = temp;
              }
              catch (NumberFormatException nfe) {
                 portField.setText((new Integer(port)).toString());
                 mainFrame.repaint();
              }
           }
        }
     });
  pane.add(portField);
  optionsPane.add(pane);

  pane = new JPanel(new FlowLayout(FlowLayout.LEFT));
  pane.add(new JLabel());
  optionsPane.add(pane);

  pane = new JPanel(new FlowLayout(FlowLayout.LEFT));
  pane.add(new JLabel());
  optionsPane.add(pane);

  // Host/guest option
  buttonListener = new ActionAdapter() {
        public void actionPerformed(ActionEvent e) {

           if (connectionStatus != DISCONNECTED) {
              changeStatusNTS(NULL, true);
           }
           else {
              isAnswerMode = e.getActionCommand().equals("answers");

              // Cannot supply host IP if host option is chosen
              if (isAnswerMode) {
                 ipField.setEnabled(false);
                 ipField.setText("localhost");
                 hostIP = "localhost";
              }
              else {
                 ipField.setEnabled(true);
              }
           }
        }
     };


  // Connect/disconnect buttons
  JPanel buttonPane = new JPanel(new GridLayout(1, 2));
  buttonListener = new ActionAdapter() {
        public void actionPerformed(ActionEvent e) {
           // Request a connection initiation
           if (e.getActionCommand().equals("connect")) {
              changeStatusNTS(BEGIN_CONNECT, true);
           }
           // Disconnect
           else {
              changeStatusNTS(DISCONNECTING, true);
           }
        }
     };
  connectButton = new JButton("Connect");
  connectButton.setMnemonic(KeyEvent.VK_C);
  connectButton.setActionCommand("connect");
  connectButton.addActionListener(buttonListener);
  connectButton.setEnabled(true);
  disconnectButton = new JButton("Disconnect");
  disconnectButton.setMnemonic(KeyEvent.VK_D);
  disconnectButton.setActionCommand("disconnect");
  disconnectButton.addActionListener(buttonListener);
  disconnectButton.setEnabled(false);
  buttonPane.add(connectButton);
  buttonPane.add(disconnectButton);
  optionsPane.add(buttonPane);

  return optionsPane;
}

/////////////////////////////////////////////////////////////////

// Initialize all the GUI components and display the frame
private static void initGUI() {
  // Set up the status bar
  statusField = new JLabel();
  statusField.setText(statusMessages[DISCONNECTED]);
  statusColor = new JTextField(1);
  statusColor.setBackground(Color.red);
  statusColor.setEditable(false);
  statusBar = new JPanel(new BorderLayout());
  statusBar.add(statusColor, BorderLayout.WEST);
  statusBar.add(statusField, BorderLayout.CENTER);

  // Set up the options pane
  JPanel optionsPane = initOptionsPane();

  // Set up the questions and answers pane
  JPanel questionsAnswersPane = new JPanel(new GridLayout(6,1) );
  questionsAnswersPane.setBorder(new EmptyBorder(4, 4, 4, 4) );

  getText = new JTextArea(10, 20);
  getText.setLineWrap(true);
  getText.setEditable(false);
  getText.setForeground(Color.blue);
  getText.setBackground(Color.LIGHT_GRAY);
  JScrollPane chatTextPane = new JScrollPane(getText,
     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
     JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

  //Question text     
  //JPanel pane = new JPanel(new FlowLayout(FlowLayout.LEFT));
  outText = new JTextField(20);

  //getLine
  //pane.add(getLine);

  JPanel pane2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
  cbAnswer1 = new JCheckBox("Answer one ........... ");
  pane2.add(cbAnswer1);
  JPanel pane3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
  cbAnswer2 = new JCheckBox("Answer two ........... ");
  pane3.add(cbAnswer2);

  JPanel pane4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
  cbAnswer3 = new JCheckBox("Answer three ........... ");
  pane4.add(cbAnswer3);

  JPanel pane5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
  cbAnswer4 = new JCheckBox("Answer four ........... ");
  pane5.add(cbAnswer4);


  questionsAnswersPane.add(outText, BorderLayout.NORTH);
  questionsAnswersPane.add(pane2);
  questionsAnswersPane.add(pane3);
  questionsAnswersPane.add(pane4);
  questionsAnswersPane.add(pane5);
  questionsAnswersPane.add(chatTextPane, BorderLayout.SOUTH);
  questionsAnswersPane.setPreferredSize(new Dimension(200, 200));

  // Set up the main pane
  JPanel mainPane = new JPanel(new BorderLayout());
  mainPane.add(statusBar, BorderLayout.SOUTH);
  mainPane.add(optionsPane, BorderLayout.WEST);
  mainPane.add(questionsAnswersPane, BorderLayout.CENTER);

  // Set up the main frame
  mainFrame = new JFrame("Server");
  mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  mainFrame.setContentPane(mainPane);
  mainFrame.setSize(mainFrame.getPreferredSize());
  mainFrame.setLocation(200, 200);
  mainFrame.pack();
  mainFrame.setVisible(true);
}

/////////////////////////////////////////////////////////////////

// The thread-safe way to change the GUI components while
// changing state
private static void changeStatusTS(int newConnectStatus, boolean noError) {
  // Change state if valid state
  if (newConnectStatus != NULL) {
     connectionStatus = newConnectStatus;
  }

  // If there is no error, display the appropriate status message
  if (noError) {
     statusString = statusMessages[connectionStatus];
  }
  // Otherwise, display error message
  else {
     statusString = statusMessages[NULL];
  }

  // Call the run() routine (Runnable interface) on the
  // error-handling and GUI-update thread
  SwingUtilities.invokeLater(tcpObj);
}

/////////////////////////////////////////////////////////////////

// The non-thread-safe way to change the GUI components while
// changing state
private static void changeStatusNTS(int newConnectStatus, boolean noError) {
  // Change state if valid state
  if (newConnectStatus != NULL) {
     connectionStatus = newConnectStatus;
  }

  // If there is no error, display the appropriate status message
  if (noError) {
     statusString = statusMessages[connectionStatus];
  }
  // Otherwise, display error message
  else {
     statusString = statusMessages[NULL];
  }

  // Call the run() routine (Runnable interface) on the
  // current thread
  tcpObj.run();
}

/////////////////////////////////////////////////////////////////

// Thread-safe way to append to the chat box
private static void appendToChatBox(String s) {
  synchronized (toAppend) {
     toAppend.append(s);
  }
}

/////////////////////////////////////////////////////////////////

// private static void sendString(String s) {
//    synchronized (toSend) {
//      toSend.append(s + "\n");
//  }
//}

/////////////////////////////////////////////////////////////////

// Cleanup for disconnect
private static void cleanUp() {
  try {
     if (hostServer != null) {
        hostServer.close();
        hostServer = null;
     }
  }
  catch (IOException e) { hostServer = null; }

  try {
     if (socket != null) {
        socket.close();
        socket = null;
     }
  }
  catch (IOException e) { socket = null; }

  try {
     if (in != null) {
        in.close();
        in = null;
     }
  }
  catch (IOException e) { in = null; }

  if (out != null) {
     out.close();
     out = null;
  }
}

/////////////////////////////////////////////////////////////////

// Checks the current state and sets the enables/disables
// accordingly
public void run() {
  switch (connectionStatus) {
  case DISCONNECTED:
     // this line is here for testing purposes
     questionText =  null;
     connectButton.setEnabled(true);
     disconnectButton.setEnabled(false);
     ipField.setEnabled(true);
     portField.setEnabled(true);
     //answerMode.setEnabled(true);
     //questionMode.setEnabled(true);
     //getLine.setText(""); 
     //getLine.setEnabled(false);
     statusColor.setBackground(Color.red);
     break;

  case DISCONNECTING:
     connectButton.setEnabled(false);
     disconnectButton.setEnabled(false);
     ipField.setEnabled(false);
     portField.setEnabled(false);
     //answerMode.setEnabled(false);
     //questionMode.setEnabled(false);
     //getLine1.setEnabled(false);
     statusColor.setBackground(Color.orange);
     break;

  case CONNECTED:
     // this line is here for testing purposes
     questionText = outText.getText() ;
     connectButton.setEnabled(false);
     disconnectButton.setEnabled(true);
     ipField.setEnabled(false);
     portField.setEnabled(false);
     //answerMode.setEnabled(false);
     //questionMode.setEnabled(false);
     //getLine.setEnabled(true);
     statusColor.setBackground(Color.green);
     break;

  case BEGIN_CONNECT:
     connectButton.setEnabled(false);
     disconnectButton.setEnabled(false);
     ipField.setEnabled(false);
     portField.setEnabled(false);
     //answerMode.setEnabled(false);
     //questionMode.setEnabled(false);
     //getLine.setEnabled(false);
     //getLine.grabFocus();
     statusColor.setBackground(Color.orange);
     break;
  }

  // Make sure that the button/text field states are consistent
  // with the internal states
  ipField.setText(hostIP);
  portField.setText((new Integer(port)).toString());
  //answerMode.setSelected(isAnswerMode);
  //questionMode.setSelected(!isAnswerMode);
  statusField.setText(statusString);
  getText.append(toAppend.toString());
  toAppend.setLength(0);

  mainFrame.repaint();
}

/////////////////////////////////////////////////////////////////

// The main procedure
public static void main(String args[]) {
  String s;

  initGUI();

  while (true) {
     switch (connectionStatus) {
     case BEGIN_CONNECT:
        try {
           // Try to set up a server if host
           //if (isAnswerMode) {
              hostServer = new ServerSocket(port);
              socket = hostServer.accept();
           //}
           in = new BufferedReader(new 
              InputStreamReader(socket.getInputStream()));
           out = new PrintWriter(socket.getOutputStream(), true);
           changeStatusTS(CONNECTED, true);
        }
        // If error, clean up and output an error message
        catch (IOException e) {
           cleanUp();
           changeStatusTS(DISCONNECTED, false);
        }
        break;

     case CONNECTED:
        try {
           // Receive data
           if (in.ready()) {
              s = in.readLine();
              if ((s != null) &&  (s.length() != 0)) {                    
                  if (s.equalsIgnoreCase("q"))
                  {

                         out.print(questionText); 
                         out.flush();
                         toSend.setLength(0);
                         changeStatusTS(NULL, true);
                  }else
                  {
                         appendToChatBox("Answer: " + s + "\n");
                         changeStatusTS(NULL, true);
                  }                     
              }
           }
        }
        catch (IOException e) {
           cleanUp();
           changeStatusTS(DISCONNECTED, false);
        }
        break;

     case DISCONNECTING:
        // Tell other chatter to disconnect as well
        out.print(END_SESSION); 
        out.flush();
        // Clean up (close all streams/sockets)
        cleanUp();
        changeStatusTS(DISCONNECTED, true);
        break;

     default: 
        break; // do nothing
     }
  }
}
}

////////////////////////////////////////////////////////////////////

// Action adapter for easy event-listener coding
class ActionAdapter implements ActionListener {
public void actionPerformed(ActionEvent e) {}
}

////////////////////////////////////////////////////////////////////

提前非常感谢您的时间和帮助


共 (1) 个答案

  1. # 1 楼答案

    套接字连接将与协议一样正常工作。我建议您编写一个自定义类,通过存储问题/答案数据的连接发送,我使用它向文本冒险服务器发送文本命令,您可以让它发送整数,然后将它们转换为服务器端的字符串