Socket with JSON in java

Asked

Viewed 354 times

0

Good night to you all.

I have a problem, in case I have a software that runs on port 3333 on a particular computer, and in it has an api, only I need to recover it through another computer and get the array using Google’s GSON library. Below my code:

import com.google.gson.Gson;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by diegokappauni on 01/07/17.
 */
class Sistema {



     void criarSocket(){

        try {
            //Abrindo conexão e conectando com o servidor
        Socket  socket = new Socket("25.96.75.21",3333);



            if(socket.isConnected()){
               System.out.print("Conexão realizada com sucesso"+"\n");


                String requestjson = "{\"id\":0,\"jsonrpc\":\"2.0\",\"method\":\"miner_getstat1\"}";


                //Recebendo requisição do servidor
               ObjectInputStream objentrada = new ObjectInputStream(socket.getInputStream());
                try {

                    System.out.print(objentrada.readObject().toString());

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


                obterDados(requestjson);



                //Fechando fluxo de entrada
                objentrada.close();

                //Fecha o socket após a requisição
                socket.close();


            }else{
                System.out.print("Conexão não realizada");
            }

                    //Checa pra ver se fechou o socket após a requisição
            if(socket.isClosed()){
                System.out.print("Conexão fechada");

            }else{
                System.out.println("Conexão ainda aberta");

            }

        } catch (UnknownHostException UnknowHost){
                System.out.print("Endereço vázio ou incorreto: "+UnknowHost.getMessage());

        } catch (IllegalArgumentException PortIncorrect){
                System.out.println("Porta fora do intervalo 0 - 65535: "+PortIncorrect.getMessage());

        }
          catch (IOException ErrorCreateSocket) {
            System.out.println("Problema ao criar um socket com o servidor: "+ErrorCreateSocket.getMessage());

        }


    }

     private void obterDados(String requestjson){


        //Instancia da class GSON
        Gson gson = new Gson();

        Minerador minerador = gson.fromJson(requestjson,Minerador.class);
        System.out.println("id: "+minerador.id);
        System.out.print("json-rpc: "+minerador.jsonrpc+"\n");
        System.out.print("method: "+minerador.method+"\n");



    }

    }

The API :

REQUEST:
{"id":0,"jsonrpc":"2.0","method":"getstat1"}

RESPONSE:
{"result": ["9.3 - fe", "21", "182724;51;0", "30502;30457;30297;30481;30479;30505", "0;0;0", "off;off;off;off;off;off", "53;71;57;67;61;72;55;70;59;71;61;70", "teste.com.br", "0;0;0;0"]}
"9.3 - fe"              - versão.
"21"                    - tempo rodando.
"182724"                - valor valido.
"30502;30457;30297;30481;30479;30505"   -detalhe total
"0;0;0"                 - velocidade
"off;off;off;off;off;off"       - on ou off
"53;71;57;67;61;72;55;70;59;71;61;70" - temp
"teste.com.br"      - site
"0;0;0;0"           - quantidade invalida
  • But what’s the problem?! You don’t know how to do json treatment?!

  • Your API uses the HTTP protocol?

  • I don’t know what it’s like, if you can show an example by taking only one value, I appreciate it, and you don’t use http, it’s just tcp/ip .

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.