Java Examples

Example: Get all diseases for 'tomato'

 

Get data directly from our API using Java

package com.peat.examples;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

public class DiseaseMinimal {
    public final static String baseUrl = "https://api.peat-cloud.com/diseases/";
    public final static String apiKey = "<YOUR_API_KEY>";
    //Replace this with your country code. For example de, in, ...
    public final static String language = "en";

    public void sendJson(String plantName) {
        String charset = StandardCharsets.UTF_8.name();
        try {
            String url = baseUrl + plantName + "/" + language;
            URLConnection connection = new URL(url).openConnection();
            connection.setRequestProperty("api_key", apiKey);
            connection.setRequestProperty("Accept-Charset", charset);
            InputStream response = connection.getInputStream();

            StringWriter respBuffer = new StringWriter();
            IOUtils.copy(response, respBuffer, charset);
            JSONObject jsonResponse = (JSONObject) JSONValue.parse(respBuffer.toString());
            // get the status code
            System.out.println(jsonResponse.get("code"));
            // get the data
            JSONArray diseases = (JSONArray) jsonResponse.get("data");
            System.out.println(diseases.size());

            for(Object json: diseases) {
                JSONObject jsonObject = (JSONObject) json;
                for(Object key  : jsonObject.keySet()) {
                    StringBuilder strBuild = new StringBuilder("\t");
                    strBuild.append(key);
                    strBuild.append("\n");
                    strBuild.append("\t\t");
                    strBuild.append(jsonObject.get(key));
                    System.out.println(strBuild.toString());
                }
                System.out.println("\n\n\n");
            }

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

    public static void main(String[] args) {
        String plantName = "tomato";
        DiseaseMinimal m = new DiseaseMinimal();
        m.sendJson(plantName);
    }

}

This would print the full list of tomato pathogens and pests. The following JSON shows 'one of n' results. There are more than 170 pathogens and pests in our database, but numbers differ greatly between host species. (e.g. Plantix API contains a lot of Tomato diseases, but just a small number of pathogens that affects Raspberry due to it's little global importance - though they are very tasty).

All relevant data comes in the response[data] array

{"code": 200,
"data": [
{
"PEAT_id": 120002,
"affectedPart": [
                "LEAF",   
                "FRUIT",  
                "SHOOT"  
                ],
"alternative_treatment":"Remove and destroy infected fruits and leaves immediately. Do not compost.
                         You can install a simple, transparent rain shelter for the tomato plants with
                         the help of a tarpaulin and wooden stakes. It should be open at the sides to
                         allow ventilation of the plants. ",  

"bullet_points": "Grey or brown wrinkled stains on fruits. Hardened fruit flesh. Brown spots grow from the leaf margins.
                  White covering on the underside of leaves. ",  

"chemical_treatment": "Use fungicides based on Mandipropamid, Chlorothalonil, Fluazinam, Triphenyltin, Mancozeb to combat Late Blight. ",
"class": "Fungi",
"class_eng": "Fungi",
"disease_name": "Tomato Late Blight",
"disease_name_eng": "Tomato Late Blight",

"preventive_measures": "Try to keep plants dry. Install a simple rain shelter. Avoid waterlogging. Ventilate your greenhouse.
                        Plant more resilient varieties. Tomatoes and potatoes should not be cultivated next to each other.
                        Disinfect all tools after pruning. Use plant fortifier for general strengthening of the plants",  

"relevance": 1.0,
"scientific_name": "Phytophthora infestans",
"spread_risk": "intermediate",

"symptoms": "Brownish-green spots appear on the leaf margins and leaf tops. Later the leaves turn completely brown.
             On the underside of the leaves grows a white covering separating the healthy and dead leaf tissue.
             In some cases, the sharply delimited brown spots also appear on the stalks, along with the white covering.
             Greyish-green to dirty-brown and wrinkled stains appear on the fruits. At these spots, the fruit flesh is hardened. ",  

"tags": [
        "ccPatch",  
        "covering",  
        "wilt" ],  

Example: Request all known symptoms for a specific disease

And if you want to get all symptoms for a specific disease, you can query the api with this script and it will send you all information about all symptoms of this disease.

package com.peat.examples;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

public class SymptomsMinimal {
    public final static String baseUrl = "https://api.peat-cloud.com";
    public final static String apiKey = "<YOUR_API_KEY>";
    //Replace this with your country code. For example de, in, ..
    public final static String language = "en";

    public void sendJson(String diseaseID) {
        String charset = StandardCharsets.UTF_8.name();
        try {
            String url = baseUrl + "/disease_id/" + diseaseID + "/" + language;
            URLConnection connection = new URL(url).openConnection();
            connection.setRequestProperty("api_key", apiKey);
            connection.setRequestProperty("Accept-Charset", charset);
            InputStream response = connection.getInputStream();

            StringWriter respBuffer = new StringWriter();
            IOUtils.copy(response, respBuffer, charset);
            JSONObject jsonResponse = (JSONObject) JSONValue.parse(respBuffer.toString());
            // get the status code
            System.out.println(jsonResponse.get("code"));
            // get the data
            JSONArray diseases = (JSONArray) jsonResponse.get("data");
            System.out.println(diseases.size());

            for(Object json: diseases) {
                JSONObject jsonObject = (JSONObject) json;
                for(Object key  : jsonObject.keySet()) {
                    StringBuilder strBuild = new StringBuilder("\t");
                    strBuild.append(key);
                    strBuild.append("\n");
                    strBuild.append("\t\t");
                    strBuild.append(jsonObject.get(key));
                    System.out.println(strBuild.toString());
                }
                System.out.println("\n\n\n");
            }

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

    public static void main(String[] args) {
        //The name of the disease, where we want to get the symptoms from
        //peat_id of the disease magnesium deficiency
        String diseaseID = "700004";
        SymptomsMinimal m = new SymptomsMinimal();
        m.sendJson(diseaseID);
    }
}

Crop overview

These are all crops you can do a query for

 "species_all": [ "PEAR", "TOMATO", "GRAPE", "POTATO", "SORGHUM", "MAIZE", "PEANUT", "WHEAT", "RICE", "GRAM", "BEAN", "PIGEONPEA", "MANGO", "PAPAYA", "BANANA", "APPLE", "QUINCE", "PLUM", "APRICOT", "PEACH", "ALMONDS", "CHERRY", "GOOSEBERRY", "CURRANT", "BLACKBERRY", "BLUEBERRY", "RASPBERRY", "STRAWBERRY", "PEA", "CABBAGE", "PEPPER", "CUCUMBER", "PUMPKIN", "ZUCCHINI", "LETTUCE", "ADDITIONAL", "SPINACH", "CHICKPEA", "SWEETPOTATO", "EGGPLANT", "HERBS", "CARROT", "ONION", "GARLIC", "TURNIP", "LEEK", "CHARD", "RADIDSH", "WALNUT", "OKRA", "HERB", "MILLET", "SUGARCANE", "SOYBEAN", "RADDISH", "RADISH", "WINE", "MELON", "MANIOC", "ROOT_VEG", "BERRY", "CASHEW", "RUBBER" ] }

   

warning: this page is under active development

 

PEAT