Checking the boolean return of the php function in java

0

I'm developing a java application that requires a license to activate. I have done the function in php that checks the data in the database and returns a true if everything is right, or false if it goes wrong.

This function is in a url:

link

What I thought of is in the java application to send the license via GET to this url, which will look like this:

link

My function is picking up this key and trying to check if it is true and returning a boolean.

In short, I need to send this parameter and also get the return, in JAVA, is it possible?

    
asked by anonymous 10.11.2017 / 22:26

1 answer

1

Following the same line of reasoning as @Denis Rudnei de Souza, you can read the content returned by the server (php page) following the instructions posted at #:

import java.io.*;
import java.net.*;

public class ReadLicence {

  public static String valida(String key) throws Exception {
    StringBuilder result = new StringBuilder();
    URL url = new URL("http://localhost:8000/a.php?licenca=" + key);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    //armazena os caracteres recebidos através da rede
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;

    //readline() permite ler uma linha (\n ou \n\r) do que foi retornada
    //como resposta pelo servidor (a partir do que está em bufferreader)
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    rd.close();
    return result.toString();
  }

  public static void main(String[] args) throws Exception {
    String chave = "123571113";
    //mensagem recebida do servidor true ou false
    String resultado = valida(chave);
    System.out.println(resultado);

    if(resultado.equals("true")){
      System.out.println("licença valida");
    }else {
      System.out.println("licença invalida");
    }
  }
}

I took the liberty of adding some comments. In addition to the code posted by @Denis Rudnei de Souza, I added the treatment (not enough) to handle the server response. On the server I created a small script called a.php:

<?php
function valida_licenca($chave){
    if($chave == '123571113'){
        return 'true';
    }else{
        return 'false';
    }
  }

//a função licença retorna string pois caso fosse boolean 
//seria convertido para 0 ou 1
echo valida_licenca($_GET['licenca']);

With this I imagine that it is possible to solve your problem. Although it was better for the server to return the content in json (if you want to look at how to read json in java with the gson library.) There is an interesting tutorial here ).

A server equivalent (PHP) to work with json would be:

<?php
function valida_licenca($chave){
    if($chave == '123571113'){
        return json_encode(['ativado' => true]);
    }else{
        return json_encode(['ativado' => false]);
    }
}

echo valida_licenca($_GET['licenca']);

And the modifications in java would be:

import java.io.*;
import java.net.*;
import com.google.gson.Gson;

public class ReadLicence {

  public static String valida(String key) throws Exception {
    StringBuilder result = new StringBuilder();
    URL url = new URL("http://localhost:8000/a.php?licenca=" + key);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    //armazena os caracteres recebidos através da rede
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;

    //readline() permite ler uma linha (\n ou \n\r) do que foi retornada
    //como resposta pelo servidor (a partir do que está em bufferreader)
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    rd.close();
    return result.toString();
  }

  public static void main(String[] args) throws Exception {
    String chave = "123571113";
    //mensagem recebida do servidor true ou false
    String resultado = valida(chave);
    System.out.println(resultado);

    //a biblioteca json permite transformar um objeto json 
    //em um objeto java
    Gson gson = new Gson();
    //nesse caso no json retornado tem esse formato:
    //{ativado: boolean}. O valor do campo ativado é colocado
    //no atributo da classe Licenca, que tem o mesmo nome
    Licenca licenca = gson.fromJson(resultado, Licenca.class);
    System.out.println(licenca.ativado);

    if(licenca.ativado){
      System.out.println("licença valida");
    }else {
      System.out.println("licença invalida");
    }
  }

  private class Licenca{
    public Boolean ativado = null;
  }
}

The most complicated thing is to add the gson library to the classpath of your development environment. The jar can be downloaded here or directly here .

    
11.11.2017 / 02:04