皆さん、私は多くを検索しましたが、自分のプロジェクトでやりたいことを満たす答えは見つかりませんでした。たぶん私は何を検索するのかわからなかった:彼はそれである: プロジェクト:一定のクラス内のフィードバックJava/Androidで複数の選択肢のアンケート(可能な回答あり)を送信
私は講師が複数の選択のアンケートを(Javaアプリケーションから)送ることを可能にするプログラムを書こうとしている学生の携帯電話(アンドロイドアプリ)に回答をタッチできるように(4つの可能な回答を含む質問)「送信」をクリックしてから、教師に返信されます。
私はちょうどこれを行う方法がわかりません。私はラジオボタンをやったが、私はそれらを送る方法がわからないので(それが可能ならば)、私はさらに詰まった。 もう一つのこと..あなたが私がやろうとしているものに最も適したプロトコルのために私に助言してくれますか?私はWI-Fiを使用しましたが、私はこのポートをこことそこで使用されるようにする必要があるたびにこの問題を抱えています...
私はあなたの提案を変更する準備はできていますが、私のためのソリューション:)
これは、これまでの私のコードです:(私はこの質問を提示台無し場合は申し訳ありませんが、これは私がこれを行う初めてです)
Androidのパッケージ
Welcome.java:
package mobileapp.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.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);
}
};
}
Answer.java:
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.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();
}
}
});
}
}
welcome.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:background="@drawable/evening"
>
<TextView android:text="@string/welcomeMessage" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textStyle="bold" android:textSize="25sp" android:textColor="#FFFFFF"
android:gravity="center"
></TextView>
</LinearLayout>
Answer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideOverlay">
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:scrollbars="vertical" android:scrollbarAlwaysDrawVerticalTrack="true"
>
<TextView android:id="@+id/textQuestion1" android:layout_width="fill_parent" android:text="@string/textQuestion" android:layout_height="wrap_content"></TextView>
<RadioGroup android:id="@+id/radioGroup01"
android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="fill_parent" >
<RadioButton android:id="@+id/rButton1" android:text="@string/c1Label" android:layout_height="wrap_content" android:layout_width="fill_parent"> </RadioButton>
<RadioButton android:id="@+id/rButton2" android:text="@string/c2Label" android:layout_height="wrap_content" android:layout_width="fill_parent"> </RadioButton>
<RadioButton android:id="@+id/rButton3" android:text="@string/c3Label" android:layout_height="wrap_content" android:layout_width="fill_parent"> </RadioButton>
<RadioButton android:id="@+id/rButton4" android:text="@string/c4Label" android:layout_height="wrap_content" android:layout_width="fill_parent"> </RadioButton>
</RadioGroup>
<Button android:text="Submit" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Connect" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</ScrollView>
</LinearLayout>
label.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:textColor">#00FF00</item>
<item name="android:singleLine">false</item>
<item name="android:lines">2</item>
</style>
</resources>
のstrings.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>
これは、Androidアプリに
serverapp.javaを質問を送ることになっている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) {}
}
////////////////////////////////////////////////////////////////////
はあなたの時間と助けを事前にどうもありがとうございました。