我希望有人可以帮助我,我正在尝试创建一个将设备的 GPS 坐标传输到服务器的基本应用程序。我完全是套接字编程的菜鸟(嗯,我今年在大学里介绍了最基本的知识)。
作为垫脚石,我正在关注一个创建服务器和 android 客户端的在线教程,它旨在将用户的输入发送到服务器,然后服务器将其打印在控制台上。我越来越熟悉如何设置服务器和客户端的基础知识,即打开端口等。问题是,当点击发送时,应用程序崩溃,在模拟器上测试时,显然存在问题,但我看不到。
当我使用设备进行测试并点击发送时,控制台上没有打印任何内容。我已经附加了下面的客户端和服务器代码,下面代码中的 ip 用于模拟器,我将其更改为我的设备 ip 并对其进行测试。
*服务器*
public class Additional_Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args) {
try {
serverSocket = new ServerSocket(2001); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 2001");
}
System.out.println("Server started. Listening to the port 2001");
while (true) {
try {
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(
clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
*客户*
import android.os.Bundle;
import android.app.Activity;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class TCPclient extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String messsage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tcpclient);
textField = (EditText) findViewById(R.id.Msg); // reference to the text field
button = (Button) findViewById(R.id.bSend); // reference to the send button
// Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = textField.getText().toString(); // get the text message on the text field
textField.setText(""); // Reset the text field to blank
try {
client = new Socket("127.0.0.1", 2001); // connect to server
printwriter = new PrintWriter(client.getOutputStream(),
true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
非常感谢任何指针、建议或帮助,因为我所看到的看起来是正确的,但显然有一些不正确的地方。提前谢谢你!!!
加里
我已经按照建议编辑了上面的代码以将其放入异步任务中,模拟器没有崩溃,但是,我仍然无法将任何打印输出到控制台..有人可以帮忙吗?我在这里尝试,我只是不太习惯android,这是最好的学习方式....
public class TCPclient extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String messsage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tcpclient);
textField = (EditText) findViewById(R.id.Msg); // reference to the text field
button = (Button) findViewById(R.id.bSend); // reference to the send button
messsage = textField.getText().toString(); // get the text message on the text field
// Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textField.setText(""); // Reset the text field to blank
new commAsyncTask().execute();
}
class commAsyncTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
client = new Socket("192.168.1.2", 2001); // connect to
// server
printwriter = new PrintWriter(client.getOutputStream());
printwriter.println(messsage);
printwriter.flush();
printwriter.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
});
}
}
所以各位,有很多人比我聪明得多,非常感谢您的帮助。