1
I’m mounting a calculator equal to Ricardo Lecheta’s book for understanding of webservice, but I’m 2 mistakes caused by a bad conversion of DataInputStream
values that should be received, with the following error:
Error:(24, 32) error: constructor Calculator in class Calculator cannot be Applied to Given types; required: String,int found: Datainputstream Reason: actual and formal argument lists differ in length
I wonder 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
}
// the methods of adding into the calculator by the 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
// implementation of the example
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);
}
}
yes but Datainputstream is to read the primitive data, as the calculator parameter is 2 primitive data(string and int) the Datainputstream should receive these values no?
– Daniel Gentil
so I read I don’t need the IP, and port, I can pass the parameter as in right there and I don’t need to declare inside n1 = in.readInt(); N2 = in.readInt();
– Daniel Gentil
Correct, you need to use the in.readInt() method to read the received values (in the int case). I believe that making these fixes in the calculator builder will solve your problem
– Bart