我需要向服务器发送请求,服务器需要回复文本字符串。现在当我点击Android应用程序上的一个按钮它发送请求到我的服务器,我可以查看它,但我在Android应用程序中看不到响应。它应该在textarea中显示文本。 下面是我的Android应用程序中的代码。
package monitor;
import java.util.Enumeration;
import android.content.Context;
import android.os.Environment;
import android.text.format.Formatter;
import android.util.Log;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.ObjectOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.io.PrintWriter;
import android.os.Bundle;
import java.net.SocketException;
import java.net.UnknownHostException;
public class welcome extends AppCompatActivity {
private Socket s;
private Button status;
private Button client;
private Button log;
private TextView text;
private PrintWriter writer;
protected void onCreate(Bundle savedInstanceState) {
OutputStreamWriter request = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
setupVariables();
final TextView mTextView = (TextView) findViewById(R.id.text);
status.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Thread t = new Thread(){
@Override
public void run(){
//String serverMsg=null;
//String text = "test";
try {
System.out.println("Starting Connection");
Socket s = new Socket("10.0.2.2", 25000);
System.out.println("Connection DONE");
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("stat");
dos.flush();
// Toast.makeText(getApplicationContext(), "Message sent to the server",
//Toast.LENGTH_SHORT).show();
//Intent intent=new Intent(welcome.this,status.class);
//startActivity(intent);
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
text = br.readLine();
dos.close();
s.close();
System.out.println("Closing socket");
}
catch (UnknownHostException e){
System.out.println("There was an Unknown Erorr:");
e.printStackTrace();
} catch (IOException e) {
System.out.println("There was an IOException:");
e.printStackTrace();
}
mTextView.setText(text);
}
};
t.start(); }
});
client.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Thread t = new Thread(){
@Override
public void run(){
try {
System.out.println("Starting Connection");
Socket s = new Socket("10.0.2.2", 25000);
System.out.println("Connection DONE");
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("client");
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
text = br.readLine();
dos.close();
s.close();
System.out.println("Closing socket");
}
catch (UnknownHostException e){
System.out.println("There was an Unknown Erorr:");
e.printStackTrace();
} catch (IOException e) {
System.out.println("There was an IOException:");
e.printStackTrace();
}
mTextView.setText(text);
dos.flush();
dos.close();
s.close();
System.out.println("Closing socket");
}
catch (UnknownHostException e){
System.out.println("There was an Unknown Erorr:");
e.printStackTrace();
} catch (IOException e) {
System.out.println("There was an IOException:");
e.printStackTrace();
}
}
};
t.start();
}
});
log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent=new Intent(welcome.this,MainActivity.class); // redirecting to LoginActivity.
startActivity(intent);
}
});
}
public void onBackPressed() {
// empty so nothing happens
}
private void setupVariables() {
status = (Button) findViewById(R.id.wel2);
client = (Button) findViewById(R.id.wel3);
log = (Button) findViewById(R.id.iog);
}
}
以下是我的服务器代码。
import java.io.*;
import java.net.*;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.Runtime;
import java.util.Enumeration;
import java.io.IOException;
public class fyp4 {
private static Socket socket;
public static void main(String[] args) throws IOException
{
try
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//Socket newSock = s.accept();
String number = br.readLine();
number = number.trim();
System.out.println("Message received from client is "+number);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}