Error in implementation WebService calculator

1

I'm setting up a calculator like Ricardo Lecheta's book for webservice understanding, but I have 2 errors caused by a bad DataInputStream conversion in the values that should be received, the following error occurred:

  

Error: (24, 32) error: constructor Calculator in class Calculator can not be applied to given types; required: String, int found:   DataInputStream reason: actual and formal argument lists   length

I'd like to know what I'm doing wrong

My Main Class

   public class CalculadoraSocket extends Activity {

   private static final int PORTA = 7777;
   public static  void main(String[] args) throws IOException{


    ServerSocket serverSocket = new ServerSocket(PORTA);
    System.out.println("Socket Aberto na porta 7777");
    while(true){

        System.out.println("esperando....");
        Socket socket = serverSocket.accept();
        System.out.println("Conectou");
        new CalculadoraSocketThread(socket).start();



        }
    }
}

Class that receives the thread that is giving conversion problem

 public class CalculadoraSocketThread extends  Thread {
 private final Socket socket;


public CalculadoraSocketThread(Socket socket) {
    this.socket = socket;
}

public void run(){
    try {
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        DataInputStream in = new DataInputStream(socket.getInputStream());
        Calculadora calc = new Calculadora(in);
        calc.somar();
        calc.enviar(out);
        out.close();
        in.close();
        socket.close();

    } catch (IOException e) {
        e.printStackTrace();
    }


}// fim run


}

// methods to add in the calculator by webservice

  public  Calculadora(String ip, int porta) throws IOException{
      socket = new Socket(ip,porta);
      out = new DataOutputStream(socket.getOutputStream());
      in = new DataInputStream(socket.getInputStream());
    Log.i(CATEGORIA, "Conexao Realizada com sucesso! ");
}// fim calculadora


     public int somar (int n1, int n2) throws IOException{
    try{
        Log.i(CATEGORIA, "Enviando numeros"+n1+" e  "+n2);

        out.writeInt(n1);
        out.writeInt(n2);

        out.flush();
        Log.i(CATEGORIA,"Lendo resposta");

        soma = in.readInt();
        Log.i(CATEGORIA,"Soma:   "+soma);
        return soma;

    }finally {
        close();
    }


}

public void close() throws IOException{

    out.close();
    in.close();
    socket.close();

}
public void enviar(DataOutputStream out) throws  IOException{

    //envia a soma pro service

    out.writeInt(soma);

}// fim enviar

// example implementation

    public void onClick(View view) {
    EditText txtn1 = (EditText) findViewById(R.id.n1);
    EditText txtn2 = (EditText) findViewById(R.id.n2);
    TextView txtSoma = (TextView) findViewById(R.id.soma);
    int n1 = Integer.parseInt(txtn1.getText().toString());
    int n2 = Integer.parseInt(txtn2.getText().toString());
    try{
        Calculadora calculadora = new Calculadora(IP,PORTA);
        int soma = calculadora.somar(n1,n2);
        String txtsoma = "Soma: "+soma;
        txtSoma.setText(txtsoma);
        Log.i(CATEGORIA,String.valueOf(txtsoma));
        txtSoma.setVisibility(View.VISIBLE);


    } catch(IOException e){
        Log.e(CATEGORIA, e.getMessage(),e);

    }

}
    
asked by anonymous 08.03.2017 / 14:54

1 answer

0

Apparently you are trying to use a Calculator class constructor that receives a DataInputStream , but Java says that the constructor expected a String and an int . Java could not find the builder you wanted to use.

  

Error: (24, 32) error: constructor Calculator in class Calculator   can not be applied to given types; required: String, int found:   DataInputStream reason: actual and formal argument lists differ in   length

In your code you do this:

DataInputStream in = new DataInputStream(socket.getInputStream());
Calculadora calc = new Calculadora(in);

And your builder is this:

public  Calculadora(String ip, int porta) throws IOException{
      socket = new Socket(ip,porta);
      out = new DataOutputStream(socket.getOutputStream());
      in = new DataInputStream(socket.getInputStream());
    Log.i(CATEGORIA, "Conexao Realizada com sucesso! ");
}// fim calculadora
    
09.03.2017 / 02:50