简体   繁体   中英

how to connect android with server

I planed to connect my android application as a client with server but I do not know where can I write the code in android activity because I have number of activities ,also I need you guys to explain the port number can I change it ? if I can depend in what?

client:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SimpleClientActivity extends Activity {

 private Socket client;
 private PrintWriter printwriter;
 private EditText textField;
 private EditText ipField;
 private Button button;
 private String messsage;
 private String serverIp;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  textField = (EditText) findViewById(R.id.editText1); //reference to the text field
  button = (Button) findViewById(R.id.button1);   //reference to the send button
  ipField=(EditText) findViewById(R.id.editText2);

  //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
    serverIp=ipField.getText().toString();

    try {

     client = new Socket(serverIp, 4444);  //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();
    }
   }
  });

 }
}

Server:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

    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(4444);  //Server socket

        } catch (IOException e) {
            System.out.println("Could not listen on port: 4444");
        }

        System.out.println("Server started. Listening to the port 4444");

        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");
            }
        }

    }
}

You can create a separate Thread your Server. And in case you need reference of your activity , you can pass instance of same to it to handle callback.

You can create a server on ports 1 through 65535, until unless it is occupied by any other app.

Let me know, if it solves your problem.

您可以尝试使用asynctask和http get post请求,例如此处示例,@ Hubert回答的问题非常好。如果您需要使用套接字,请尝试查看此处此处 ,希望对您有所帮助

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM